Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 此变量未定义_Javascript_Dojo - Fatal编程技术网

Javascript 此变量未定义

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

您好,我收到了这个错误。\u单选按钮在下面的代码段中未定义(在**之间的代码)。我在这里遗漏了一些关于闭包的东西吗

_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
或诸如此类