Passing scope to forEach

You need to use Function#bind to bind a scope:

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

Note that this is not available in all browsers: you should use a shim (as provided in the link above) to add it in the browsers that don’t support Function#bind.


As dandavis points out in the comments, you can pass a value to Array#forEach as the context for the callback:

words.forEach(this.addToCount, this);

Leave a Comment