- Under the classical inheritance phenomenon, we create a new class that actually extends or reuses the properties or functions, or methods of another class that are used by several programming languages (like C, C++, Java, etc.)
- In Prototype Inheritance, an object uses the properties or methods of another object via the prototype linkage.
Following pictorial representation, containing some sample values will help us to understand Prototype Inheritance in a much better and effective way-
- In the above pictorial representation, we have taken an example to illustrate the Prototype Inheritance between a rabbit and another create prototype object which is an animal.
Now that you have understood a brief detailed description of the Prototype inheritance let us see and understand Prototype Inheritance with several following approaches-
- In this approach we will use __proto__, which is the special name for the internal and hidden prototype called [[Prototype]].
- We will store all the properties of the rabbit in the animal prototype object and thereafter we may access it whenever it is required.
- Here we will use Object.setPrototypeOf() method takes two parameters first one is the object which is to have its prototype set and the second one is the object’s new prototype.
- Thereafter we have declared two objects and using those two objects, we will set one of the objects as the prototype object for another object.