Inheritance in JavaScript. Detailed Walk Thorough of Inheritance - by Rupesh Mishra - Medium
Inheritance in JavaScript. Detailed Walk Thorough of Inheritance - by Rupesh Mishra - Medium
Save
Inheritance in JavaScript
Detailed walk thorough of inheritance in JavaScript
JavaScript does not have classes like other languages. It uses the concept of
prototypes and prototype chaining for inheritance. In this post, we will discuss how
we can achieve inheritance in JavaScript using Prototypes.
1.6K 10
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 1/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
Prototype Chaining
Prototype chaining means an object’s dunder proto or proto property will point to
another object instead of pointing to the constructor function prototype . If the
other object’s dunder proto or proto property points to another object it will result in
the chain. This is called Prototype Chaining.
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 2/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 3/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
Above code defines two constructor functions, SuperType and SubType . By default,
SubType.prototype has a constructor function which points to the constructor
function itself and proto property which inherits the default object properties.
Above line rewrites the default prototype or the dunder proto property of the
SubType constructor function and makes SubType.prototype to point to an object of
SuperType constructor function.
This means that all the properties and methods that exist on an instance of
SuperType will now exist on SubType.prototype also. This means that now, SubType
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 4/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
After the default prototype of SubType constructor function has been overwritten,
by using the above line of code we add a new method getSubAge() on top of what
was inherited from SuperType , to the prototype object of SubType constructor
function.
Note: New methods must be added to the SubType after the inheritance because
inheritance overwrites the existing prototype of SubType
Console output
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 5/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
To fix this issue, we use the constructor to inherit the instance properties and
prototype chaining to inherit methods and share properties
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 6/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
Let’s try to understand the code, we have defined a SuperType constructor function
with firstName , lastName , and friends as instance properties, then we have defined
a superName property on the prototype of SuperType .
Now, let’s look at how we have defined the SubType constructor function
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 7/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
So far we have just inherited all the instance properties of the SuperType
constructor function, but the shared properties and methods of the SuperType
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 8/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
constructor function is still not inherited. We inherit them using the above lines of
code.
Once the above lines of code are executed, we have inherited all the properties of
the SuperType constructor function
When we execute the above line of code, all the three parameters (Virat, Kohli and
26) are passed to the SubType constructor function. SubType constructor function
then calls SuperType constructor function using call SuperType.call(this, firstname,
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 9/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
Thus as of now, there are properties with the subTypeObj1 object (firstName,
lastName, and age). Currently, SubType constructor function has the following
methods and shared properties in its prototype property:
1. getSuperName()
2. getSubAge
If you like my articles and find them useful, feel free to buy me a coffee. Thanks!
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 10/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
Other articles:
4. Why Progressive Web Apps are great and how to build one
6. Service Workers
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 11/12
09/05/2023, 15:26 Inheritance in JavaScript. Detailed walk thorough of inheritance… | by Rupesh Mishra | Medium
https://medium.com/@happymishra66/inheritance-in-javascript-21d2b82ffa6f 12/12