Trying to wrap my mind around prototypes in Javascript, specifically Node.js, with a simple test.
function Lint() {
this.input = 'foo';
events.EventEmitter.call(this);
}
Lint.prototype.dirs = function (dirs) {
_.each(dirs, this.files);
}
Lint.prototype.files = function (dir) {
console.log(this.input); // trying to get 'foo', returns undefined
}
var lint = new Lint();
lint.dirs(['js', 'js/views']);
Lint.prototype.files logs undefined because this isn't referring to the instance of Lint. What am I missing here?
The only solution I can think of, that works, is passing around the initial this from Lint.prototype.dirs to each other function. I'm pretty sure there's a better way.