修正hashtableobj.set("length","0") bug 可以设置key忽略大小写 可以clone hashtable对象 可以 使用obj.valueOf("key","defalutvalue") 设置默认值等等 欢迎修正bug 复制代码 代码如下: <BR>// Authors Birdshome, 麻袋@博客园 改版 phito,彭海涛 <BR>Object.prototype.Clone = function() <BR>{ <BR>var objClone; <BR>if ( this.constructor == Object ) objClone = new this.constructor(); <BR>else objClone = new this.constructor(this.valueOf()); <BR>for ( var key in this ) <BR>{ <BR>if ( objClone[key] != this[key] ) <BR>{ <BR>if ( typeof(this[key]) == 'object' ) <BR>{ <BR>objClone[key] = this[key].Clone(); <BR>} <BR>else <BR>{ <BR>objClone[key] = this[key]; <BR>} <BR>} <BR>} <BR>objClone.toString = this.toString; <BR>objClone.valueOf = this.valueOf; <BR>return objClone; <BR>} <BR>function Hashtable() { <BR>this.clear = hashtable_clear; <BR>this.containsKey = hashtable_containsKey; <BR>this.containsValue = hashtable_containsValue; <BR>this.get = hashtable_get; <BR>this.isEmpty = hashtable_isEmpty; <BR>this.keys = hashtable_keys; <BR>this.put = hashtable_put; <BR>this.remove = hashtable_remove; <BR>this.size = hashtable_size; <BR>this.toString = hashtable_toString; <BR>this.values = hashtable_values; <BR>this.hashtable = new Object(); <BR>this.set = hashtable_set; <BR>this.valueOf = hashtable_valueOf; <BR>this.clone = hashtable_clone; <BR>this.ignoreupperlower = true; <BR>//是否忽略大小写 <BR>} <BR>/*=======Private methods for internal use only========*/ <BR>function hashtable_clone(){ <BR>return this.Clone(); <BR>} <BR>function hashtable_put(key, value) { <BR>if (this.ignoreupperlower && typeof(key) == "string") { <BR>key = key.toUpperCase(); <BR>} <BR>if (key == null || value == null) { <BR>throw "NullPointerException {" + key + "},{" + value + "}"; <BR>} else { <BR>this.hashtable[key] = value; <BR>} <BR>} <BR>function hashtable_set(key, value) { <BR>if (this.ignoreupperlower && typeof(key) == "string") { <BR>key = key.toUpperCase(); <BR>} <BR>if (this.containsKey(key)) { <BR>this.remove(key); <BR>} <BR>this.put(key, value); <BR>} <BR>function hashtable_get(key) { <BR>if (this.ignoreupperlower && typeof(key) == "string") { <BR>key = key.toUpperCase(); <BR>} <BR>return this.hashtable[key]; <BR>} <BR>function hashtable_valueOf(key, defvalue) { <BR>var ret = this.get(key); <BR>if (typeof(ret) == "undefined") { <BR>return defvalue; <BR>} <BR>return ret; <BR>} <BR>function hashtable_remove(key) { <BR>if (this.containsKey(key)) { <BR>delete this.hashtable[key] ; <BR>} <BR>} <BR>function hashtable_isEmpty() { <BR>return (parseInt(this.size()) == 0) ? true: false; <BR>} <BR>function hashtable_size() { <BR>var size = 0; <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>if (this.hashtable[i] != null) { <BR>size++; <BR>} <BR>} <BR>return size; <BR>} <BR>function hashtable_toString() { <BR>var result = ""; <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>if (this.hashtable[i] != null) { <BR>result += "{" + i + ":" + this.hashtable[i] + "}\n"; <BR>} <BR>} <BR>return result; <BR>} <BR>function hashtable_clear() { <BR>this.hashtable = new Object(); <BR>} <BR>function hashtable_containsKey(key) { <BR>if (this.ignoreupperlower && typeof(key) == "string") { <BR>key = key.toUpperCase(); <BR>} <BR>var exists = false; <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>if (i == key && this.hashtable[i] != null) { <BR>exists = true; <BR>break; <BR>} <BR>} <BR>return exists; <BR>} <BR>function hashtable_containsValue(value) { <BR>var contains = false; <BR>if (value != null) { <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>if (this.hashtable[i] == value) { <BR>contains = true; <BR>break; <BR>} <BR>} <BR>} <BR>return contains; <BR>} <BR>function hashtable_values() { <BR>var values = new Object(); <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>if (this.hashtable[i] != null) values.push(this.hashtable[i]); <BR>} <BR>return values; <BR>} <BR>function hashtable_keys() { <BR>var keys = new Object(); <BR>for (var i in this.hashtable) { <BR>if(typeof(this.hashtable[i])=="function"){ <BR>continue; <BR>} <BR>keys.push(i); <BR>} <BR>return keys; <BR>} <BR>function test() { <BR>var ht = new Hashtable(); <BR>ht.put("3", "Jackson"); <BR>ht.put("2", "Tom"); <BR>ht.put("4", 3); <BR>ht.set("length", 445555); <BR>ht.set("ddd", "ddd"); <BR>ht.set("index", "ddd"); <BR>var et = ht.toString(); <BR>ht.ignoreupperlower = false; <BR>//忽略大小写 <BR>ht.clear(); <BR>ht.put("3", "Jackson"); <BR>ht.put("2", "Tom"); <BR>ht.remove("2"); <BR>ht.put("4", 3); <BR>ht.set("length", 5); <BR>//如果用new Array的话该项会设置Array的长度 <BR>ht.set("index", "ddd"); <BR>ht.set("ddd", "ddd"); <BR>alert(et + "" + ht.toString() + "" + ht.size()); <BR>var cloneobj=ht.clone(); <BR>alert(cloneobj.toString()); <BR>} <BR> 如果你想使用功能更好的hashtable和hashset请下载: http://xiazai.jb51.net/201012/yuanma/jshashtable.rar