2
Function ConstrA () {
    EventEmitter.call(this);
}
util.inherits(ConstrA, EventEmitter);

vs

Function ConstrA() {}
util.inherits(ConstrA, EventEmitter);

Is there something that the EventEmitter.call(this) does that is required?

ciso
  • 2,887
  • 6
  • 33
  • 58

2 Answers2

2

Is there something that the EventEmitter.call(this) does that is required?

Apparently, yes:

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;
};

Since all the methods that use ._events do a check for its existence I wouldn't expect much to break if you did omit the call, but I'm not sure whether this holds true in the future.

There are enough other constructors that do not tolerate to be omitted, so it's good practice to simply always call the constructor when constructing an instance.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • So the emit and on/once functions would still work and this EventEmitter constructor call is mostly for future expansion? I do see the domain portion of init, but being new don't understand that yet. Just wanting to learn why I need to call the Constructor when the prototype has all emit and on/once functions in it. – ciso Apr 01 '14 at 16:28
  • 1
    In general, because every new instance might need to be initialized - that's what constructors are for. Calling them is definitely a good practice. From a quick glance at the code EventEmitters might work fine without, but I wouldn't suggest to omit it. – Bergi Apr 01 '14 at 16:30
  • Ok, I can understand the good practice part of this. So my initial thought that it isn't required is correct, but it's good practice to call the constructor in my constructor because there may be reasons for it that are not obvious unless you look at the code? – ciso Apr 01 '14 at 16:33
  • 2
    What @Bergi said. If you want to make sure your EventEmitter is properly constructed, make sure to call the constructor. Not doing so would at best be unsupported. – Jason Apr 01 '14 at 16:33
  • 3
    If you want to access parts of `EventEmitter` without creating an entire instance of it (with the constructor) then you should know the code well enough to understand why and how you would want to do so. I can understand why there might be reasons to do so, but you should require an affirmative reason to omit it rather than require an affirmative reason to do it. – Jason Apr 01 '14 at 16:36
1

util.inherits grabs the entire parent prototype, but you lose the constructor. For this reason, the inheriting constructor will often call the parent constructor with the current this as the context, just like you see in your first example.

Jason
  • 13,606
  • 2
  • 29
  • 40