Search code examples
javascriptnode.jsprototypal-inheritance

What is Event Emitter Call?


I am learning Node Js. I have encountered a snippet of code in my book that stated as follow:

var EventEmitter = require("events").EventEmitter;
var inherits = require('util').inherits;

//Custom class
function Foo(){
   EventEmitter.call(this);
}
inherits(Foo, EventEmitter);

Foo.prototype.connect = function(){
    this.emit('connected');
}

var foo = new Foo();
foo.on('connected', function(){
    console.log("connected raised!');
}

foo.connect();

My question is that what is "call" here do? Why does the class Foo inherits from EventEmitter? Does that mean that Foo is a child of Event Emitter? If so whu must it be a child of the EventEmitter? I have found another question in Stackoverflow regarding the call (What does EventEmitter.call() do?) However, I did not understand the answer provided... Thanks

Source of the code: Beginning Node.js by Basarat Ali Syed


Solution

  • The line of code:

    EventEmitter.call(this);
    

    calls the constructor of the object you are inheriting from which allows the EventEmitter code to initialize its portion of this object which is part of the inheritance process in Javascript.

    EventEmitter() is the constructor function for the EventEmitter object. Since you need to call that constructor with the same this as your new object, you must use either .call() or .apply() with that constructor in order to cause the correct this to be used. Since there are no arguments you are passing to the constructor, .call() is the simplest way to call it.

    You must call the EventEmitter() constructor in order to allow it to properly initialize its portion of the object that was created with new Foo(). When using inheritance in Javascript, multiple separate object definitions are using the same object to store their properties and methods so each much initialize their portion of the object and that initialization is started by calling the constructor of the object that you inherited from.

    Here's a good reference on the topic of chaining constructors.


    It appears from some of your comments that you don't understand what the point of the inheritance in your code is. That code allows you to create an object type Foo that has your own methods on it, but that object ALSO is an eventEmitter and has all the capabilities of an EventEmitter such that it can trigger events, respond to events, etc... This is called "inheritance" where you inherit the functionality of some other object with your own custom object. To make inheritance work, your code does two things. With the inherits(Foo, EventEmitter); line of code, it inherits the prototype of the other object so that it will have all the same methods available and with EventEmitter.call(this);, it calls the constructor of the inherited object so that object can initialize itself properly.

    You may want to read a couple of reference articles on Javascript inheritance:

    Introduction to Object-Oriented JavaScript

    Inheritance and the prototype chain

    Understanding JavaScript Inheritance

    What is "inheritance" in Javascript?

    Inheritance: Object Oriented Programming