这里面的this指的什么 有点懵了 然后是怎么个原理
举一个更简洁的例子:
function A() {
this.name = "111" //这里的this,称为this1
}
function B() {
A.call(this) //这里发生了:1、执行了构造函数A 2、用这个this(称为this2)替换了A()上面的this;
}
// this2.name="111" 执行了A()可以看成这样
var b = new B(); //执行到这里,1、B()里面的this2确定了,就是指向new B(),也就是B;
alert(b.name)如果感觉似懂非懂,再看这个:
function A() {
this.name = "111"
}
function B() {
this.name = "222"
}
var b = new B();
(function c() { //c是个自执行函数
A.call(b) //执行A函数,用b去代替A里面的this, 因此A里面的语句可以看成:b.name="111"
})()
alert(b.name)