javascript - js对象函数如何不劫持callback函数的this指向?
阿神
阿神 2017-04-10 15:07:40
0
2
310
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`
阿神
阿神

闭关修行中......

reply all(2)
洪涛

this.bfun = new Bfun(this.callBack.bind(this));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob...

黄舟
function Afun() {
    this.bfun = new Bfun( this.callBack.bind(Afun) );
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template