簡単なテストで、Javascript、特に Node.js のプロトタイプに頭を悩ませようとしています。
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
this
Lint のインスタンスを参照していないため、ログは未定義です。ここで何が欠けていますか?
私が考えることができる唯一の解決策は、最初のthis
fromLint.prototype.dirs
をお互いの関数に渡すことです。もっと良い方法があると確信しています。