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

时间:2020-12-12 12:20:20

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是未定义的。

4 个解决方案

#1


35  

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:

    使用第三个参数_.each()来传递上下文:

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

#2


5  

You have to pass this as context for _.each call like 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的文档

#3


1  

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

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

short version:

简洁版本:

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

每次调用函数时,它的值都会改变。要修复,设置另一个等于此的变量并引用它

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;
    }
};

#4


0  

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);

#1


35  

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:

    使用第三个参数_.each()来传递上下文:

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

#2


5  

You have to pass this as context for _.each call like 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的文档

#3


1  

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

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

short version:

简洁版本:

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

每次调用函数时,它的值都会改变。要修复,设置另一个等于此的变量并引用它

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;
    }
};

#4


0  

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);