看代码
function A () {
this.init();
}
functin test () {
console.log('test');
}
A.prototype.init = function () {
this.test = test.bind(this) // 看这里
this.prototype.test = test(this) // 还是这里
}
看上面代码中我标记的地方,我想动态绑定一个外部函数作为A
对象的方法,init
方法里的this
是指function A
呢还是A.prototype
?
这样就可以了。
以上面这样方式调用init方法的话,init方法中的this指向a
以上面这样方式调用init的话,this指向A.prototype
我觉得
init()里的this是根据谁来调用它来决定的
a.init()时this指向a
A.prototype.init()时this指向A.prototype