function Persion(name) {
var self = this;
self.name = name;
console.log(self);
console.log(this);
}
Persion.prototype = {
before: function() {
// var self = this;
console.log('before' + self);
console.log('before' + this);
return this;
},
after: function(){
// var self = this;
console.log('after' + self);
console.log('after' + this);
return this;
}
};
var a = new Persion('Tom');
a.before().after();
var a = new Persion('Tom')的时候self和this还是指向Persion,怎么执行a.before().after()的时候self就指向了Window了,本身就是想用self保存this的,我并没有人为改变self,那为什么self的值会变,什么原理?
补充:
是最上面那句var self= this;在调试的时候发现self由Persion变成了window,before和after里面我注释掉声明是发现self未声明时输出的不是undefined而是window
相关分类