DEV Community

Cover image for What is the Prototypal Inheritance in JavaScript?
Hamza Nadeem
Hamza Nadeem

Posted on

What is the Prototypal Inheritance in JavaScript?

For understanding Prototypal Inheritance in JavaScript let's first understand what Inheritance is.

Inheritance is one of the basic concepts of OOP (Object Oriented Programming). Inheritance is the capability of one class to inherit capabilities or properties from another class .let's take an example. We are humans, We inherit certain properties from the class 'Human' such as the ability to speak, breathe, eat, drink, etc.

In JavaScript, objects have a special hidden property [Prototype], that is either null or references another object. That object is called “a prototype”.

When we want to read a property from an object, and it’s missing, JavaScript automatically takes it from the prototype, this is called “prototypal inheritance”.

let animal = {
eats: true
};
let rabbit = {
jumps: true
};

rabbit.proto = animal;

alert( rabbit.eats ); // true
alert( rabbit.jumps ); // true

Top comments (0)