这是生成 get set 方法的代码,里面的which哪位大虾给讲讲呀
function user(properties){
for(var i in properties){(function(which){
var p = i;
which["get"+p]=function(){
return properties[p];
};
which["set"+p]=function(val){
properties[p]=val;
};
})(this);
}
}
function user(properties){
for(var i in properties){(function(which){
var p = i;
which["get"+p]=function(){
return properties[p];
};
which["set"+p]=function(val){
properties[p]=val;
};
})(this);
}
}
//....
})(b)
临时定义一个函数并且马上传参调用。a是参数列表。b是传过去的值,用来代替a的
function user(properties){
for(var i in properties){
(function(which){//which指的是下面传入的参数
var p = i;
which["get"+p]=function(){
return properties[p];
};
which["set"+p]=function(val){
properties[p]=val;
};
})(this);//参数this指向该方法:user
}
}//定义对象
var o = new Object();
o.name = 'lihui_shine';//生成user对象
var u = new user(o);
alert(u.getname());
</script>
{a:1,b:"x",
geta:function(){return this.a},
seta:function(val){this.a=val;},
getb:function(){return this.b;},
setb:function(val){this.b=val;}
}
有点像.NET或Java中的字段封装成属性
for(var i in properties){
alert('i->'+i);
(function(which){//which指的是下面传入的参数
var p = i;
which["get"+p]=function(){
return properties[p];
};
which["set"+p]=function(val){
properties[p]=val;
};
})(this);//参数this指向该方法:user
}
}//定义对象
var o = new Object();
o.name = 'lihui_shine';
o.meth = function(){ alert('a');};//生成user对象
var u = new user(o);alert(u['getname']());
u.getmeth()();学习了。蛮复杂的。