/**
* V1.0
* HashMap javascript版
* 2013-09-04
* var map = new $.HashMap();
map.put("age","20");
map.put("name","yk1");
alert(map.get("age"));
alert(map.toString());
map = new $.HashMap({"username":"ycyk_168","password":"123456"});
map.put("age","20");
map.put("name","yk1");
alert(map.get("username"));
alert(map.get("password"));
alert(map.get("age"));
alert(map.get("name"));
alert(map.toString());
*
*/
jQuery.HashMap = function(param) {
var _map = typeof(param)=="undefined" ? [{}] : [param];
/**
* 向HashMap中添加数据
*/
_map.put = function(key,value){
this[0][key] = value;
};
/**
* 从HashMap中获取指定的Key的值
*/
_map.get = function(key){
return this[0][key];
};
/**
* HashMap中是否包含指定的Key
*/
_map.containsKey = function(key){
return this.get(key) == null ? false : true;
};
/**
* 从HshMap中删除Key
*/
_map.remove = function(key){
delete this[0][key];
};
/**
* 获取HashMap的大小
*/
_map.size = function(){
var count = 0;
for(var item in this[0]){
count ++;
}
return count;
};
/**
* 将HashMap转换为string的数据
*/
_map.toString = function(){
var str = "{";
for(var item in this[0]){
str += ""+ item +":"+ this[0][item] +",";
}
str = $.util.removeEnd(str,",") + "}";
return str;
};
/**
* 将HashMap转换为JSON格式的数据
*/
_map.toJson = function(){
return this[0];
};
return _map;
};
JavaScript模拟的HashMap,请大家多多提意见!