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

前端 未结 4 641
無奈伤痛
無奈伤痛 2021-02-05 08:32

Here is my code:

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

TextClass.prototype = {
    SetTexts: function (texts) {
        for (var i = 0; i < te         


        
4条回答
  •  被撕碎了的回忆
    2021-02-05 08:47

    You have to pass this as context for _.each call like this:

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

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

提交回复
热议问题