您好我收到错误this._radioButtons未定义(**之间的代码)在下面的代码段中。我在这里找不到关闭的东西吗?
_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"));
}));
});
},
答案 0 :(得分:2)
您在array.forEach(choices, function(choice) { /* your code here*/ })
回调函数内,因此this
指的是函数。添加this
作为强制上下文的第三个参数:
array.forEach(choices, function(choice) {
// YOUR CODE HERE
}, this); // => here
答案 1 :(得分:0)
在我个人看来,可以通过在错误的功能范围之外声明某种指针来改善可读性,例如
// 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_adustChoices
或诸如此类......