Prototype and Inheritance in JS

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__
Term | What It Refers To | Where It Lives | Purpose |
prototype | A property of constructor functions | Only on functions | Used to define properties for objects created from the constructor |
__proto__ | A reference to an object's internal prototype | Exists on all objects | Points 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__”.
Subscribe to my newsletter
Read articles from Manish Saw directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
