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;
}
}
문제는 내시경이요 생쥐라. '어떻게' 또는 '이' 를 아다토카운트 유지됩니까 지나갈 수 있는 방법이 다른 it 작동합니까?
사용해야 할 때 '기능은 # 바인딩하지' 바인딩하려면 범위:
words.forEach(this.addToCount.bind(this));
참고로 이 모든 브라우저에서 사용할 수 없습니다. a 씨는 콩지름에 (제공한 위 링크에서) '기능을 추가할 수 있는 t 지원, # # 39 이 브라우저를 don& 바인딩하지'.
다 데이비스 점 등 정보를 전달할 수 있습니다 값이 '어레이입니다 # 포리치' 에 대한 컨텍스트입니다 콜백하는:
words.forEach(this.addToCount, this);
이 같은 일이 시도하시겠습니까. # 39 번 '에서', '보다는' _this i& 사용할 뿐만 아니라, '이동' 그래서 it& # 39 의 인사이드라면 아다토카운트 I& # 39 번 '카운트워즈'. 카운트워즈 끄는 '마감' 에 포함된.
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;
});
}
}