Hello coders today was a lot more reviewing of JavaScript fundamentals again in the Node.js course along with learning inheritance within event emitters. The fundamentals talked about today were ES6, object.create, template literals, .call, and .apply.

What is inheritance?

The order in which these fundamental JavaScript concepts were presented set up how inheritance works with event emitters. By using function constructors and object.create it places a prototype object in between two new objects in which you want an object to inherit properties. This creates a prototype chain from one object to another and will have its prototype be the properties/features that you’re actually looking to add on. Because of how prototype chains work we will still be able to access it further down the chain if we wanted more objects to have access to properties as well.

To sum it up object one can have access to properties and methods on object two because of object.create. Object.create made the empty middle prototype object.

Inheritance basic syntax

The first step that I learned in creating inheritance in the event emitter is to create a variable equal to require core modules in Node.js. In the example in the course our first two variables were…

var EventEmitter = require('events');
var util = require('util');

After requiring core modules from Node.js the next step was to create a function constructor.

function Greetr() {
         this.greeting = 'Hello world!' ;
}                 

Then, we used the inherit function.

util.inherits(Greetr, EventEmitter);

This inherit function is essentially saying that Greetr should also have access to the methods and properties on the prototype property of EventEmitter. At this point, you can create new objects and properties with object.create on the fly while using the prototype chain inheritance.

Super constructor

At this point, we are able to create a new property or a method on an object being created on the fly with function constructors. However, this leaves out something important. We are able to inherit down the prototype chain but this is not a consistent pattern since it is not consistent with every new object we create. There is a method of inheriting properties and methods directly to the new object.

In the above syntax .this was the new object being created whenever we say new then function constructor. This is incomplete inheritance however one line of code fixes this.

EventEmitter.call(this);

This is equivalent to a super constructor meaning the constructor that I’m inheriting from. Using .call or .apply gives the new object that I’m working on properties and methods to that new object. In the example above the this keyword inside the EventEmitter will be the same this inside Greetr. While at the same time the prototype chain is still set up the same way, so that properties and methods are still available to all objects that are created from this function constructor.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: