Javascript 此变量未定义
您好,我收到了这个错误。\u单选按钮在下面的代码段中未定义(在**之间的代码)。我在这里遗漏了一些关于闭包的东西吗Javascript 此变量未定义,javascript,dojo,Javascript,Dojo,您好,我收到了这个错误。\u单选按钮在下面的代码段中未定义(在**之间的代码)。我在这里遗漏了一些关于闭包的东西吗 _adjustChoices: function(choices) { // TODO Tear down all the old radion buttons and their change handlers. debugger; this._radioButtons = []; t
_adjustChoices: function(choices) {
// TODO Tear down all the old radion buttons and their change handlers.
debugger;
this._radioButtons = [];
this._changeHandlers = [];
array.forEach(choices, function(choice) {
var radioButton = new RadioButton(lang.mixin({
name: this._clusterName
}, choice));
**this._radioButtons.push(radioButton);**
this._changeHandlers.push(connect.connect, radioButton, "onChange", lang.hitch(this, function(value) {
// TODO Figure out which radio button is selected and get its value.
//var radioButton = ????;
this.set("value", radioButton.get("checked"));
}));
});
},
您在
数组中。forEach(选项,函数(选项){/*您的代码在这里*/})
回调函数,因此此
引用该函数。添加此
作为强制上下文的第三个参数:
array.forEach(choices, function(choice) {
// YOUR CODE HERE
}, this); // => here
在我个人看来,可以通过在bug函数范围之外声明某种指针来提高可读性,如
// code code code
_adjustChoices: function(choices) {
var _this = this;
// rest of your code...
array.forEach(choices, function(choice) {
//stuff you do
var radioButton = new RadioButton(lang.mixin({
name: _this._clusterName //access it this way
}, choice));
}
}
这样,就可以访问每个this
-指针,甚至可以将其命名为this\u adustChoices
或诸如此类