Inheritance in JavaScript

Inheritance in JavaScript allows objects to inherit(actually link) to properties and methods of other objects, creating a hierarchical relationship between them.

Meher Howji avatar image
··· Likes

JavaScript's prototypal system is inspired by the language Self. Now, Self was the first one to implement it. Lua, ActionScript, JScript, Cecil and many other languages exist that implement this pattern.

In prototype system, there are no classes. Yes, JS has no classes and ES6 classes is just a syntactic sugar, internally JS does prototype-based wiring for you.

Class-based languages are based on a deep-rooted duality of Classes & Object Instances. For instance, if we create an object of the class Vehicle, such as Red Car, and send it a message to deliver super heavy materials, it is not feasible because Red Car is not equipped for that and it is something that's wired in Vehicle class. This highlights the importance of subclassing, which enables creating specialized classes, such as Sports Car and Truck, with their own unique capabilities.

However, predicting the future qualities of objects and classes is challenging, and systems can become rigid when the initial design does not anticipate future changes. And this was one of the motivation to create prototype feature, as such use cases occur in real world development all the time.


Objects are much more than just maps in JavaScript. They are not just a collection of key value pair. They are very generic in nature for instance, you can link them to create the features of inheritance with no classes at all.