Sitemap
Raghav Vian Panthi

Raghav Vian Panthi

Prototype & Prototype Chain | inheritance in JavaScript

Prototype

Prototype is the approach where a normal object can get inherit another object properties and methods.

const person = {
greet() {
console.log("Ram Ram ");
}
};
const Shyam= Object.create(person);
Shyam.greet();

Here the Shyam will inherit the feature of the Person.

Get Raghav Vian Panthi’s stories in your inbox

Join Medium for free to get updates from this writer.

Prototype Chain

Prototype Chain is the path JavaScript follows to find a property or method when it is not found directly on an object.

const animal = {
eat() {
console.log("Eating");
}
};

const dog = Object.create(animal);

dog.bark = function () { // prototype seperate branch created
console.log("Barking");
};

dog.bark();// here we called that branch
dog.eat(); // here we call the upper object prototype

approach how flow of the prototype chain.

dog
↓
animal
↓
Object.prototype
↓
null // end of the chain .

--

--