日期:2014-05-16  浏览次数:20441 次

一个js简单模拟Map结构
   一个js简易模拟Map结构,够用了。
     
<!--
	var Map=function(){
	    this.mapArr={};
		this.arrlength=0;

		//假如有重复key,则不存入
	    this.put=function(key,value){
		  if(!this.containsKey(key)){
		   this.mapArr[key]=value;
		   this.arrlength=this.arrlength+1;
		  }
		}
		this.get=function(key){
		  return this.mapArr[key];
		}

		//传入的参数必须为Map结构
		this.putAll=function(map){
           if(Map.isMap(map)){
		       var innermap=this;
		       map.each(function(key,value){
			      innermap.put(key,value);
			   })
		   }else{
		     alert("传入的非Map结构");
		   }
		}
		this.remove=function(key){
              delete this.mapArr[key];
		      this.arrlength=this.arrlength-1;
		}
		this.size=function(){
             return this.arrlength;
		}

		//判断是否包含key
		this.containsKey=function(key){
			  return (key in this.mapArr);
		}
		//判断是否包含value
        this.containsValue=function(value){
              for(var p in this.mapArr){
			     if(this.mapArr[p]==value){
				    return true;
				 }
			  }
			  return false;
		}
		//得到所有key 返回数组
		this.keys=function(){
		   var keysArr=[];
		   for(var p in this.mapArr){
		     keysArr[keysArr.length]=p;
		   }
		   return keysArr;
		}
		//得到所有value 返回数组
		this.values=function(){
		  var valuesArr=[];
		   for(var p in this.mapArr){
		     valuesArr[valuesArr.length]=this.mapArr[p];
		   }
		   return valuesArr;
		}
		
		this.isEmpty=function(){
		   if(this.size()==0){
		     return false;
		   }
		   return true;
		}
		this.clear=function(){
             this.mapArr={};
			 this.arrlength=0;
		}
		//循环
		this.each=function(callback){
	        for(var p in this.mapArr){
			 callback(p,this.mapArr[p]);
		   }

		}
        

	}
	//判断是否是map对象
	Map.isMap=function(map){
	  return  (map instanceof Map);
	}


	var map=new Map();
	map.put("afei",25);
	map.put("yaoming",31);
	map.put("pp",2);
	map.put("bill","55");
	map.remove("afei");

    var map2=new Map();
	map2.put("003",333);
	map2.put("004",444);
	map.putAll(map2);


    map.each(function(key,value){
	    alert(key+" : "+value);
	})


	
 //-->