【问题标题】:Passing scope to forEach将范围传递给 forEach
【发布时间】:2013-11-01 19:11:57
【问题描述】:

我正在尝试使用回调方法 addToCount 而不是 forEach 中的匿名函数。但我无法访问其中的this.count(返回undefined)。

function Words(sentence) {
  this.sentence = sentence;
  this.count = {};
  this.countWords();
}

Words.prototype = {
  countWords: function() {
    var words = this.sentence.split(/\W+/);
    words.forEach(this.addToCount);
  },
  addToCount: function(word) {
    word = word.toLowerCase();
    if (word == '') return;
    if (word in this.count)
      this.count[word] += 1;
    else
      this.count[word] = 1;
  }
}

我认为问题在于范围。我怎样才能将this 传递给addToCount 或者有其他方法可以让它工作吗?

【问题讨论】:

  • words.forEach(this.addToCount, this);
  • 完美简洁

标签: javascript


【解决方案1】:

你需要使用Function#bind来绑定一个作用域:

words.forEach(this.addToCount.bind(this));

请注意,这并非在所有浏览器中都可用:您应该使用 shim(如上面链接中提供的)将其添加到不支持 Function#bind 的浏览器中。


正如 dandavis 在 cmets 中指出的那样,您可以将值传递给 Array#forEach 作为回调的上下文:

words.forEach(this.addToCount, this);

【讨论】:

  • @lonesomeday - 我建议交换上面的两个答案,因为如果你不使用 ES6,第二个可能是今天更好的方法。
【解决方案2】:

试试这样的。我使用了that 而不是_this,但我也移动了addToCount,所以它在countWords 里面。这会将countWords 变成一个包含它的闭包。

Words.prototype = {
  countWords: function() {
    var that = this, words = this.sentence.split(/\W+/);
    words.forEach(function(word) {
        word = word.toLowerCase();
        if (word == '') return;
        if (word in that.count)
          that.count[word] += 1;
        else
          that.count[word] = 1;
      });
  }
}

【讨论】:

  • 这是我一开始就有的,我正在尝试重构它。
猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多