javascript 为什么我的变量在 Underscore.js 的每个函数中都未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13356203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:32:17  来源:igfitidea点击:

Why is my variable undefined inside the Underscore.js each function?

javascriptvariablesunderscore.js

提问by CJe

Here is my code:

这是我的代码:

TextClass = function () {
    this._textArr = {};
};

TextClass.prototype = {
    SetTexts: function (texts) {
        for (var i = 0; i < texts.length; i++) {
            this._textArr[texts[i].Key] = texts[i].Value;
        }
    },
    GetText: function (key) {
        var value = this._textArr[key];
        return String.IsNullOrEmpty(value) ? 'N/A' : value;
    }
};

I'm using the Underscore.js library and would like to define my SetTexts function like this:

我正在使用 Underscore.js 库并想像这样定义我的 SetTexts 函数:

_.each(texts, function (text) {
    this._textArr[text.Key] = text.Value;
});

but _textArr is undefined when I get into the loop.

但是当我进入循环时 _textArr 是未定义的。

回答by DCoder

In JavaScript, the function context, known as this, works rather differently.

在 JavaScript 中,称为 的函数上下文的this工作方式截然不同

You can solve this in two ways:

您可以通过两种方式解决此问题:

  1. Use a temporary variable to store the context:

    SetTexts: function (texts) {
      var that = this;
      _.each(texts, function (text) {
        that._textArr[text.Key] = text.Value;
      });
    }
    
  2. Use the third parameter to _.each()to pass the context:

    SetTexts: function (texts) {
      _.each(texts, function (text) {
        this._textArr[text.Key] = text.Value;
      }, this);
    }
    
  1. 使用临时变量来存储上下文:

    SetTexts: function (texts) {
      var that = this;
      _.each(texts, function (text) {
        that._textArr[text.Key] = text.Value;
      });
    }
    
  2. 使用第三个参数_.each()来传递上下文:

    SetTexts: function (texts) {
      _.each(texts, function (text) {
        this._textArr[text.Key] = text.Value;
      }, this);
    }
    

回答by Andrey Kuzmin

You have to pass thisas context for _.eachcall like this:

您必须像这样传递this作为_.each调用的上下文:

_.each(texts, function (text) {
    this._textArr[text.Key] = text.Value;
}, this);

See the docs for http://underscorejs.org/#each

请参阅http://underscorejs.org/#each的文档

回答by mkoryak

thisin javascript does not work the same way as you would expect. read this article: http://www.digital-web.com/articles/scope_in_javascript/

this在 javascript 中的工作方式与您预期的不同。阅读这篇文章:http: //www.digital-web.com/articles/scope_in_javascript/

short version:

简洁版本:

the value of thischanges every time you call a function. to fix, set another variable equal to thisand reference that instead

this每次调用函数时的值都会发生变化。修复,设置另一个变量等于this并引用它

TextClass = function () {
    this._textArr = {};
};

TextClass.prototype = {
    SetTexts: function (texts) {
        var that = this;
        for (var i = 0; i < texts.length; i++) {
            that._textArr[texts[i].Key] = texts[i].Value;
        }
    },
    GetText: function (key) {
        var value = this._textArr[key];
        return String.IsNullOrEmpty(value) ? 'N/A' : value;
    }
};

回答by JayCrossler

Note that you can also pass things other that "this". For example, I do something like:

请注意,您还可以传递“this”之外的其他内容。例如,我做这样的事情:

var layerGroupMasterData = [[0],[1,2,3],[4,5],[6,7,8,9],[10]];

_.each(layerGroupMasterData,function(layerGroup,groupNum){
    _.each(layerGroup, function (layer, i) {
            doSomethingThatComparesOneThingWithTheOverallGroup(layerGroupMasterData,layer);
    },layerGroups);
},layerGroupMasterData);