Skip to main content

The Prototype Chain - How Inheritance Works

Updated 2 min readMarkdown
The Prototype Chain - How Inheritance Works

JavaScript uses prototypal inheritance, which means objects can share properties and methods with other objects. This helps in writing reusable and efficient code. To understand this better, we’ll explain the prototype chain with a simple real-world example.


Problem without Prototype Chain?

  • Assume objectA has 20 key-value pairs and follows a certain structure. Now, if we want objectB to have the same structure, one way is to manually assign properties from objectA. However, this approach may lead to issues. To avoid this, we use the prototype chain.

What is Prototype Chain?

  • Imagine a family where habits and traditions are passed down. The grandfather has certain traditions that everyone in the family follows. The father inherits these traditions but also develops his own unique habits. The child learns from the father, following both the grandfather's traditions and the father's habits.

  • This is how things work in JavaScript—when something is missing, it is searched for in the family line until it is found.

  • This is how things work in JavaScript! When something is missing, it is searched for in the family line until it's found.

  • Bachha can access prototypes of Papaji and Dadaji and Papaji can access prototypes of Dadaji.


Prototype Chain - Code Example

// Grandparent Object
let grandfather = {
  surname: "Patel",
  sayHello: function () {
    console.log("Hello from the grandfather!");
  }
};

// Parent Object inheriting from Grandparent
let father = Object.create(grandfather);
father.occupation = "Computer Engineer";

// Child Object inheriting from Father
let child = Object.create(father);
child.hobby = "Painting";

console.log(child.surname);  // Inherited from grandfather -> Output: "Smith"
console.log(child.occupation);  // Inherited from father -> Output: "Engineer"
child.sayHello();  // Inherited method from grandfather -> Output: "Hello from the grandfather!"
  • The child doesn’t have the surname property, so JavaScript looks up to its prototype (father).

  • The father doesn’t have surname either, so JavaScript checks the grandfather.

  • It finds surname: "Patel" in the grandfather and returns it.


Visualizing the Prototype Chain

  • Every object in JavaScript ultimately links to Object.prototype, which provides common methods like toString(), hasOwnProperty(), etc.

Why is the Prototype Chain Useful?

  1. Dynamic Behavior – If you update a method in a prototype, all inherited objects reflect the change automatically.

  2. Code Reusability – Instead of copying methods into every object, they can inherit from a common prototype.

  3. Memory Efficient – Methods are stored in a shared prototype, saving memory. (DRY principle 😀).


Questions for you

  • What is the output of this three console.log, write in comment section.

      const arr = [];
      console.log(arr.__proto__);
      console.log(arr.__proto__.__proto__);
      console.log(arr.__proto__.__proto__.__proto__);
    

ChhotiBachhi++ 🚀

About the author

  • A web developer, certified in Java. Pursuing a BCA in the third year and currently learning full-stack web development from Chaicode.

array k prototypes Object k prototypes (Everything in js is object) null

More from this blog