Prototype Inheritance in JavaScript
Last Updated :
06 Dec, 2024
Prototype inheritance in JavaScript allows objects to inherit properties and methods from other objects. Each object in JavaScript has an internal link to another object called its prototype. This chain of prototypes forms the prototype chain.
When you access a property or method on an object, JavaScript first checks the object itself. If the property or method isn’t found, it moves up the prototype chain until it finds the property or reaches the end of the chain (null).
JavaScript
const parent = {
greet: function () {
console.log("Hello from the parent object!");
}
};
const child = Object.create(parent);
child.sayHi = function () {
console.log("Hi from the child object!");
};
child.greet();
child.sayHi();
OutputHello from the parent object!
Hi from the child object!
Prototype Chain
The prototype chain is the mechanism that JavaScript uses to resolve properties and methods. If an object doesn’t have a requested property, the JavaScript engine searches up the prototype chain.
JavaScript
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
console.log(`${this.name} makes a sound.`);
};
const dog = new Animal("Buddy");
console.log(dog.name);
dog.speak();
OutputBuddy
Buddy makes a sound.

In the above pictorial representation, we have taken an example to illustrate the Prototype Inheritance between a rabbit and another create prototype object which is an animal. We will set the rabbit's prototype object as an animal prototype object wherein we will store all the values of rabbit for a purpose that if in the case in while rabbit properties are missing then JavaScript will automatically take it from animal prototype object.
Now that you have understood a brief detailed description of Prototype inheritance let us see and understand Prototype Inheritance with following approaches -
- Using __proto__
- Using Object.setPrototypeOf() method
1. Using __proto__
In this approach, we will use __proto__, which is the special name for the internal and hidden prototype called [[Prototype]]. We will store all the properties of the rabbit in the animal prototype object and thereafter we may access it whenever it is required. This __proto__ is a bit old as well as an outdated approach that exists for some historical reasons associated with JavaScript.
JavaScript
let animal = {
animalEats: true,
};
let rabbit = {
rabbitJumps: true,
};
// Sets rabbit.[[Prototype]] = animal
rabbit.__proto__ = animal;
console.log(rabbit.animalEats);
console.log(rabbit.rabbitJumps);
In this approach, we will use the new JavaScript methods to implement JavaScript Prototype Inheritance, Here we will use Object.setPrototypeOf() method takes two parameters first one is the object which is to have its prototype set and the second one is the object's new prototype. Thereafter we have declared two objects and using those two objects, we will set one of the objects as the prototype object for another object.
JavaScript
let rabbit = {
rabbitJumps: true,
};
let animal = {
animalEats: true,
};
Object.setPrototypeOf(rabbit, animal);
console.log(rabbit.animalEats);
console.log(rabbit.rabbitJumps);
Similar Reads
Prototypal Inheritance using __proto__ in JavaScript Every object with its methods and properties contains an internal and hidden property known as [[Prototype]]. The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another objec
2 min read
JavaScript Inheritance Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class or object to derive properties and behaviours from another. In JavaScript, inheritance is like a parent-child relationship, where objects, functions, or classes can inherit properties and methods from oth
6 min read
JavaScript Prototype In JavaScript, everything is an object, including functions, arrays, and strings, which are specialized types of objects. JavaScript follows a prototype-based system, where objects inherit properties and methods from other objects through prototypes. This prototype mechanism plays a key role in how
9 min read
JavaScript Array prototype Constructor The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will be available for every array. When constructing a property, All arrays will be given the property, and its value, as default. Syntax: Array.
2 min read
How does JavaScript .prototype work ? In JavaScript when adding behaviour to objects, then if creating multiple objects using the constructor (calling the constructor function with the 'new' keyword) then the new keyword is the one that is converting the function call into constructor call and every time a brand new empty object is crea
4 min read