skip to content
Profile image
Dharmendra Kashaudhan

Inheritance and prototypes in JavaScript

/ 1 min read

JavaScript objects have a hidden property [[Prototype]] & it is called prototype.

Prototype

Prototypal inheritance

There are many ways to set [[Prototype]], one of them is __proto__:

let bird = {
fly: true
};
let eagle = {
eats: true
};
// sets eagle.[[Prototype]] = bird
eagle.__proto__ = bird;

Now when we log the property eagle.fly it will log true.

console.log(eagle.fly) // true
console.log(eagle.eats) // true

The references can be made to multiple levels.

It has some limitations:

  1. References can’t be circular. JavaScript will throw an error if we try to assign __proto__ in a circle.
  2. The value of __proto__ can be only object of null. No other value is allowed.

Note: this blog is under progress