Java Script Prototypes

Table of contents
What is Prototype
In Java Script, most of features or methods we use are not available even in Java script. Java script takes them from browser and uses them. And Browser don’t have time to write those methods for all variables or data. So Browser writes code only once for each data type’s prototype which stores all the methods or functions and properties that data type should have. So to make it easy, let we selected “object” datatype, but keep in mind that, it is available for all data types.
Prototype is like the place where all the methods of Object is available. And when we create any variable of object datatype, then js just copies all those properties and methods from the Parent object and pastes them on that created variable object’s prototype, and now we can use all those methods in that variable also.
How Prototype works
When we code, we just write things like:
But in the Backend/behind the scene, Functions like “concat” are found by Java script by firstly checking that does the string have a method like “concat” and (only in case of objects, there is just chances that, that method is available in form of one of the property of that object). So when it’s not found directly, then java script searches on his prototype(that he got from his parent String) and finally here it will be found.
Prototype Inheritance/Chain
This is done automatically, whenever we creates any variable, then its prototype is inherited from the parent object. But If we wanna to modify or customize it, then we are also able to do that, but it’s not a good practices. But for learning purpose, let’s inherit prototypes:
So here we inherited the prototype of greet 1 into greet2. You can try it by yourself to learn more.
Hope you Understood About Prototypes and its Inheritance/chain. Meet you next time, till that , be happy.
Subscribe to my newsletter
Read articles from Manish Saw directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
