Prototype and Inheritance in JS

Manish SawManish Saw
2 min read

What is Prototype Inheritance

Prototype Inheritance in Java script is a way to use or access the methods and properties of one object to another object through a shared prototype.

Use of Prototype Inheritance

It can be used to:

  • Save memory because it’s shared

  • Enhance the code reusability

  • Enables a form of OOPs(Object Oriented Programming) in Java Script

Examples of Prototype Inheritance

Here We Inherited all the properties and methods available at “toy1” object to “toy2” object. And there are some ways also to Inherit like this:

This is the other way to Inherit, but this or using “prototype” method is not preferred, because they are modifying the prototype which is not good at all.

In these Examples our request of any method or property is following this path:

And In this path, all in-built function and methods are found inside “prototypes” and all the function and methods declared by us are found inside the objects.


Difference between Prototype and __proto__

TermWhat It Refers ToWhere It LivesPurpose
prototypeA property of constructor functionsOnly on functionsUsed to define properties for objects created from the constructor
__proto__A reference to an object's internal prototypeExists on all objectsPoints to the prototype of the constructor that created the object

👨‍🏫 In Simple Words:

  • prototype: The template or blueprint for objects created by a constructor.

  • __proto__: The link from an object to its prototype (used for inheritance).

Code Example:

Here’s what’s happening:

  • Person.prototype is the blueprint.

  • manish.__proto__ points to that blueprint.


Hope you Understood about Prototype Inheritance and the difference between “prototype” and “__proto__”.

0
Subscribe to my newsletter

Read articles from Manish Saw directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Saw
Manish Saw