function User(properties){
for(var i in properties){
(function(which){
var p = i;
which["get" + p] = function(){
return properties[p];
};
which["set" + p] = function(){
properties[p] = val;
}
})(this);
}
}
var user = new User({name: 'bob',age: 14});
alert(user.name);
alert(user.getname());
这里的(function(which){})(this)写法是什么意思?(this)代表什么意思?(which)又是什么意思呢
for(var i in properties){
(function(which){
var p = i;
which["get" + p] = function(){
return properties[p];
};
which["set" + p] = function(){
properties[p] = val;
}
})(this);
}
}
var user = new User({name: 'bob',age: 14});
alert(user.name);
alert(user.getname());
这里的(function(which){})(this)写法是什么意思?(this)代表什么意思?(which)又是什么意思呢
(this)是实参,传进去的参数 这里的this是指user对象的引用
which是形参
一开始this=window是因为你当时的上下文是全局
也就是说this是和上下文有关的(context)你用firebug跟踪的时候,跟踪的其实是当前运行的上下文