JavaScript模拟的HashMap,请大家多多提意见!

/**
 * 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;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值