1
试图用一个简单的测试将我的思想围绕在Javascript中的原型,特别是Node.js。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
是不是指皮棉的实例。我在这里错过了什么?
我能想到的唯一的解决办法,这样的作品,是经过初始this
从周围向Lint.prototype.dirs
彼此的功能。我很确定有更好的方法。
权,当然:)你为什么CW呢? – alex 2012-04-14 13:59:41
@alex:这是我现在的事情。我所有的答案都是CW,所以如果你愿意的话可以随意选择。 ;-) – 2012-04-14 14:01:45
非常感谢!有趣的是,为什么这种变化返回错误? 'VAR this_lint =这一点;'' _.each(迪尔斯,this_lint.files);' – 2012-04-14 14:11:13