Prototypical Inheritance in Javascript
In Javascript we don’t have classes, rather we only have objects. So if we want to implement inheritance with objects we can get the help of prototypical inheritance.
Before talking about prototypical inheritance, we all should stay on the same pitch in terms of javascript prototype. So,
Prototype in Javascript
Suppose we have one object john
. We have another object person
.
john
is a type of person
, right?
We will add the common behaviors to the person
object, then later somehow we will link this person
object with the john
object. Now we can refer person
as the prototype of john
object.
Prototype is just a parent of another object.
Every object in javascript (except a single one) has a prototype object. Now the question is how this prototype looks like?
We can get the help of the Chrome dev tool to see how a prototype looks like.
In the above screenshot, we can see the prototype of person
object inside __proto__
property. Yes, this is the prototype of person
…