What is Prototype Chaining in JavaScript

Amit Dhiman
Stackademic
Published in
2 min readNov 21, 2023

--

Photo by Arnold Francisca on Unsplash

Prototype chaining is a mechanism in JavaScript that allows objects to inherit properties and methods from other objects by sharing a common prototype. In JavaScript, each object has an associated prototype object, and these prototypes form a chain. When a property or method is not found on an object, JavaScript looks up the prototype chain to find it in the prototype of the object or in the prototypes of its ancestors.

Here’s how prototype chaining works:

Object Prototypes:

  • Every object in JavaScript is associated with a prototype object.
  • The prototype object is a regular object itself, and it may have its own prototype.

Prototype Chain:

  • When you access a property or method on an object, JavaScript first checks if the object has that property or method.
  • If the property or method is not found on the object, JavaScript looks up the prototype chain.

Inheritance:

  • If the property or method is not found on the immediate prototype, JavaScript continues to search in the prototype of the prototype (and so on), forming a chain of prototypes.
  • This process continues until the property or method is found or until the end of the prototype chain is reached.

Object.create():

  • The Object.create() method is often used to create objects with a specified prototype.
  • This method creates a new object with the specified prototype, establishing a direct link between the object and its prototype.

Here’s a simple example:

// Prototype Object
const animal = {
makeSound: function() {
console.log('Generic Animal Sound');
}
};
// Object with Prototype
const dog = Object.create(animal);
dog.bark = function() {
console.log('Woof! Woof!');
};
// Object with Prototype
const cat = Object.create(animal);
cat.meow = function() {
console.log('Meow! Meow!');
};
// Using the objects
dog.makeSound(); // Inherited from the prototype
dog.bark(); // Defined on the object
cat.makeSound(); // Inherited from the prototype
cat.meow(); // Defined on the object

In this example, dog and cat are objects with their own properties (bark and meow), but they also inherit the makeSound method from the shared animal prototype. This forms a prototype chain: dog and cat inherit from animal, and animal itself may have its own prototype, forming a chain of prototypes.

It’s important to note that the prototype chain is dynamic, and changes to the prototype object are immediately reflected in the objects that inherit from it. This mechanism is at the core of JavaScript’s prototypal inheritance.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.

--

--

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Angular, Javascript, Typescript, Html and CSS.