As JcFx points out, your scope of this is different from the callback of each .
However, if you use the anonymous bold arrow function, which will use the lexical rules for this to get what you are looking for:
render(){ collection.each((x) => this.add(x)); }
Compiles the following JavaScript:
X.prototype.render = function () { var _this = this; collection.each(function (x) { return _this.add(x); }); }
Another option is to explicitly bind the add call for the desired this :
render(){ collection.each(this.add.bind(this)); }
Johnnyhk
source share