所以,下面是一个非常简单的示例,其中我将 that
设置为 this
因此我可以使用 myObj
。我将如何集成 Function.prototype.bind,这样我才能了解将来如何使用它?
var myObj = {
specialFunction: function () {
},
anotherSpecialFunction(){
},
getAsyncData: function (cb) {
// an operation
cb();
},
render: function () {
var that = this;
this.getAsyncData(function () {
that.specialFunction();
that.anotherSpecialFunction();
});
}
};
myObj.render();
最佳答案
只需在您想要的方法上使用绑定(bind)并指定上下文
render: function () {
this.getAsyncData(this.specialFunction.bind(this));
}
render: function () {
this.getAsyncData(someFunction.bind(this));
function someFunction() {
this.specialFunction();
this.anotherSpecialFunction();
}
}
关于javascript - 将 JavaScript 的 .bind() 与回调函数集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227872/