Function ConstrA () {
EventEmitter.call(this);
}
util.inherits(ConstrA, EventEmitter);
VS
Function ConstrA() {}
util.inherits(ConstrA, EventEmitter);
EventEmitter.call(this)是否需要这样做?
解决方法:
Is there something that the EventEmitter.call(this) does that is required?
Apparently,是的:
function EventEmitter() {
EventEmitter.init.call(this);
}
…
EventEmitter.init = function() {
this.domain = null;
if (EventEmitter.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
};
由于使用._events的所有方法都检查它是否存在,如果你省略了调用,我不会期望太多打破,但我不确定这是否适用于将来.
有足够的其他构造函数不能容忍被省略,因此在构造实例时简单地总是调用构造函数是一种好习惯.