Inheritance in Node.js

Read other posts on Node.js here

In this post we will take a look on inheritance of objects in Node.js. We will learn to use utility module to achieve inheritance in Node.js. However keep in mind that you can write plain JavaScript to achieve inheritance in Node as well. You can use Object.create() to inherit one object to another in Node.js also.

In this post we will learn to use util module to achieve inheritance in Node.js. Very first you need to import util module in your application


After importing util module, let us say you have an object as below,

function Student()
{ = "G Block";
 this.age = 40;

Just for demonstration let us add function in object using prototype,

Student.prototype.Display= function(){
 console.log( + " is " + this.age + " years old");

Next we are going to create ArtsStudent object which will inherit Student object.

function ArtsStudent()
 this.subject = "music";


Second line of code in ArtsStudent object is very important,


If you don’t call constructor of parent object as shown in above code snippet then on trying to access properties of parent object will return undefined.

In last line ArtStudent inherits Student using util.inherits() function ,


Next you can create instance of ArtsStudent and call function of parent object as below,

var a = new ArtsStudent();

Inheritance can be chained to any order. If you want you can inherit object from ArtsStudent as well. Inherited object will contain properties from both ArtsStudent and Student objects. So let us consider one more example,

function ScienceStudent()
 this.lab = "Physics";

var b = new ScienceStudent();

In above example ScienceStudent object inherits both Student and ArtsStudent objects. In this way you can work with inheritance in Node.js using util module. I hope you find this post useful. Thanks for reading.

6 thoughts on “Inheritance in Node.js

  1. Is this a new publicity stunt? This is one of the most idiotic articles I’ve ever seen on the internet. Why the hell does anyone in the world want to talk about inheritance in node? People know that node is nothing but pure JavaScript running on the server, so obviously, all JS properties hold good. If you dare, explain how node works and not silly things as this post.

  2. for sure it is not publicity stunt 🙂 you are right that JavaScript Inheritance concepts are applicable in Node. I have also mentioned that in post. Purpose of this post is to show util package. hope it helps

  3. Hi Dhananjay, it was a life savior for a noob like me ..thanks for the article..simple and precise!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s