js 模拟java 中 的map

//js模拟map

Map = {
obj : {},
put : function(key , value){
this.obj[key] = value;
},
get : function(key){
return this.obj[key];
},
eachMap : function(fn){
for(var arr in this.obj){
fn(arr,this.obj[arr]);
}
}
};

var map = Map;

//利用js对象的key,value的唯一性特性实现模拟

//put 方法
map.put('key1',9);
map.put('key2', false);
map.put('key3',new Date());

//遍历map中的值,以回调函数的方式操作,回调函数就是把自己的函数作为参数传入对象并添加对象的属性作为参数执行
map.eachMap(function(key,value){
alert(key+' :'+value);
});
alert(map.get('key2'));

 

********************************************************************************************************************************************************

// call方法的简单模拟与实现

把函数放到某一个对象中使用
//function 方法
function test1(a , b){
return a+b;
}

// 自定义的对象
function Obj(x, y){
this.x = x ;
this.y = y ;
return x*y;
}

var o = new Obj(10 , 20);
o.method = test1 ;
alert(o.method(o.x , o.y));
delete o.method;

//alert(test1.call(o,o.x ,o.y));

posted on 2013-12-15 23:54  a_badegg  阅读(216)  评论(0编辑  收藏  举报

导航