js中map使用
HashMap是一种非常使用的、应用广泛的数据类型。代码如下:
function HashMap(){
this.map = {};
}
HashMap.prototype = {
put : function(key , value){
this.map[key] = value;
},
get : function(key){
if(this.map.hasOwnProperty(key)){
return this.map[key];
}
return null;
},
remove : function(key){
if(this.map.hasOwnProperty(key)){
return delete this.map[key];
}
return false;
},
removeAll : function(){
this.map = {};
},
keySet : function(){
var _keys = [];
for(var i in this.map){
_keys.push(i);
}
return _keys;
}
};
HashMap.prototype.constructor = HashMap;
hashmap的使用
var hashMap = new HashMap();
//向hashmap中存放值
hashMap.put(a,b);
//从map中取值
hashMap.get(a);
