<script> function hashtable(){ /*存放key value的对象容器*/ this.container = new Object(); /*添加一个key value*/ this.put = function(key,value){ if(typeof(key)=="undefined"){ return falsue; } if(this.contains(key)){ return false; } this.container[key] = typeof(value) == "undefined" ? "null":value; }; /*删除指定key*/ this.remove = function (key){ delete this.container[key]; }; /*获取指定key*/ this.get = function(key){ return this.container[key]; }; /*获取table元素的个数*/ this.size = function(){ var size = 0; for(var attr in this.container){ size++; } return size; }; /*清空table表*/ this.clear = function(){ for(var attr in this.container){ delete this.container[attr]; } }; /*列表到字符串*/ this.toString = function(){ var str = ""; for(var attr in this.container){ str+= ","+attr+"="+this.container[attr]; } if(str.length >0){ str = str.substr(1,str.length); } return "{"+str+"}"; }; /*判断key 是否存在*/ this.contains = function(key){ return typeof(this.container[key])!="undefined"; //疑似错误 }; } /*调用例子*/ var obj = new hashtable(); obj.put("xiaoke","aaaaaaaaaa"); obj.put("a",124); obj.clear(); alert(obj.toString()); </script>
转载于:https://blog.51cto.com/hlist/1393706