Prototype chaining

Sagar Kumar JhaSagar Kumar Jha
2 min read

Prototype chaining in JavaScript is a mechanism used to implement inheritance by linking objects through their prototypes. If a property or method is not found on an object, the JavaScript engine looks up the chain via the object's [[Prototype]] (accessible using Object.getPrototypeOf(obj) or the __proto__ property).

When using constructor functions, methods and properties meant to be shared are added to the constructor’s prototype object (e.g., ConstructorFunction.prototype).

So:

  • obj.__proto__ or Object.getPrototypeOf(obj) gives the prototype of the object instance

  • ConstructorFunction.prototype is the object that will become the prototype of all instances created with new ConstructorFunction()

Prototype Chaining in JavaScript (Interview-Style Explanation)

Prototype chaining is the mechanism by which JavaScript objects inherit properties and methods from other objects. Every object in JavaScript has an internal link to another object called its [[Prototype]]. This chain continues until it reaches null.

This is how inheritance is implemented in JavaScript — it's how one object can "look up" properties or methods defined on another.

📌 Key Interview Points

  • Each object has a [[Prototype]], accessible via Object.getPrototypeOf(obj) or obj.__proto__.

  • Constructor functions have a .prototype property — this is used to assign the prototype of instances created with new.

  • When accessing a property, JavaScript checks the object first, then follows the prototype chain if the property isn't found.

function Animal(name) {
  this.name = name;
}

Animal.prototype.sayHello = function () {
  return `Hi, I'm ${this.name}`;
};

function Dog(name, breed) {
  Animal.call(this, name); // Call parent constructor
  this.breed = breed;
}

// Set up inheritance via prototype chain
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function () {
  return `${this.name} says Woof!`;
};

const dog = new Dog("Buddy", "Golden Retriever");

console.log(dog.bark());      // Buddy says Woof!
console.log(dog.sayHello());  // Hi, I'm Buddy

🧠 Summary of Key Points

ConceptAccessed ByPurpose
Instance’s prototypeObject.getPrototypeOf(obj) or obj.__proto__Shows what object the instance inherits from
Constructor’s prototypeConstructor.prototypeWhere shared methods are defined
Prototype chainingFollows the [[Prototype]] chainEnables inheritance in JS
0
Subscribe to my newsletter

Read articles from Sagar Kumar Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sagar Kumar Jha
Sagar Kumar Jha

Softwares are art and I am an artist