因此,对于下面的代码,我尝试将外部函数的“this”关键字传递到draw_bldText(...)函数中。但我该怎么做呢?当我在执行中调用 this 时,“this”指的是函数作用域内。我想要 this.piece 中的“this”关键字。
我希望我的问题有意义,我是 javascript 新手
this.myDrawFunction;
this.piece = this;
switch(image) {
case "blank":
break;
case "bldText":
myDrawFunction = {
execute : function() {
draw_bldText(this, this_popup.context, this_popup.focus);
}
};
. . .
. . .
default:
break;
}
最佳答案
您需要将局部变量绑定(bind)到 this
,并使用它以便将其保存在闭包中:
some_func() {
var that = this;
myDrawFunction = {
execute: function() {
draw_bldText(that, this_popup.context, this_popup.focus);
}
};
}
关于javascript - 将 "this"引用传递给 javascript 中的方法的提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915676/