JS Shorts — Prototypal Inheritance

Prakhar Jaiswal
2 min readDec 9, 2020

The way of extending the common properties from one object to another though their prototype property is called prototypal inheritance.

The inheritance in JavaScript is a bit different from other languages. It’s more of a delegation rather than inheriting the properties. Everything in JavaScript is an Object in essence. Each of these objects holds a __proto__ property that is a reference to another object, which is called the object’s prototype. When a property is accessed on an object and if it’s not found on that object, the JavaScript engine looks for that property in the object’s __proto__ property. If it’s not found here, then it looks in the __proto__'s __proto__ property and so on until it reaches the end of the prototype chain. This behaviour is what we refer to as Prototypal Inheritance in JavaScript.

Notice how we are able to access the login method from Admin even though it’s not defined there. This is possible due to the Prototypal Inheritance.

Notice the structure of both the objects. The userOne’s __proto__ has the login method and admin’s __proto__ has the deleteUser method along with another __proto__ property which points to the User’s __proto__ property.

Image by LEEROY Agency from Pixabay

— That’s all for today

--

--