gpt4 book ai didi

javascript - this.variable 未定义

转载 作者:行者123 更新时间:2023-12-02 19:01:53 26 4
gpt4 key购买 nike

您好,我在以下代码片段中收到错误 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"));
}));
});
},

最佳答案

您位于 array.forEach(choices, function(choice) {/* your code here*/}) 回调函数内,因此 this 引用该函数。添加 this 作为强制上下文的第三个参数:

array.forEach(choices, function(choice) { 

// YOUR CODE HERE

}, this); // => here

关于javascript - this.variable 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com