代码改变世界

js插件-Map插件

2013-12-23 23:18  低调de草原狼  阅读(256)  评论(0编辑  收藏  举报

自己封装的jsMap插件,虽然很小,但是基本实现了功能。

/**
 * Map
 */
(function(){
	function jMap(){
		//私有变量
		var arr = {};
		//增加
		this.put = function(key,value){
			arr[key] = value;
		}
		//查询
		this.get = function(key){
			if(arr[key]){
				return arr[key]
			}else{
				return null;
			}
		}
		//删除
		this.remove = function(key){
			//delete 是javascript中关键字 作用是删除类中的一些属性
			delete arr[key]
		}
		//遍历
		this.eachMap = function(fn){
			for(var key in arr){
				fn(key,arr[key])
			}
		}
	}
	var country = new jMap();
	country.put("01","ZG");
	country.put("02","HG");
	country.put("03","MG");
	country.put("04","TG");
	//alert(country.get("01"))
	country.remove("01")
	//alert(country.get("01"))
	
	country.eachMap(function(key,value){
		document.write(key+"-->"+value+"<br>");
	})
})()