js对象函数如何不劫持callback函数的this指向?



 function Afun(){
    this.bfun = new Bfun(this.callBack);
}

Afun.prototype.callBack = function(){
    console.log(this);//这个函数被`Bfun`调用时,this指的是`Bfun`,如何让它指为`Afun`?
}

function Bfun(callBack){
    callBack();
}

new Afun();//输出`Bfun`,但是我希望是`Afun`

JavaScript prototype

☆.大虾牛┽ 9 years, 9 months ago

 function Afun() {
    this.bfun = new Bfun( this.callBack.bind(Afun) );
}

机动战士葛平 answered 9 years, 9 months ago

高坂桐乃. answered 9 years, 9 months ago

Your Answer