Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 7, 2022 03:58 pm GMT

Prototypes in JavaScript!

Image description

Prototypes is one of the most important pillars in JavaScript! In this article we will understand what are prototypes,what are global objects, what is this proto property and how inheritance works in JavaScript underneath the hood.
So lets get started without further ado,
When we create a function in JavaScript, JavaScript Engine creates two objects:-
The Function Object itself
The Prototype Object
Image description
For eg consider this,
Let say we create a function f1() as follows,
Image description

Then what JS Engine does is, it creates two objects as mentioned above,
The f1 function object itself and f1 function's Prototype Object,
Image description

Now what's this prototype that you can see inside this f1 Object?
Well, JS Engine creates a property on function Object that points to the function's Prototype Object.
We can access the f1 object directly by doing this,
Image description

However if we want to access the f1's prototype Object we can access it by using that prototype property,
Image description

Now suppose we create a new object from that f1 function
Image description

and if we access it, we can see that there is something called as proto
Image description

This proto is property that JS Engine creates for every object! Well,this proto property points to the Prototype Object of the function(In this case it will point to f1's Prototype Object)
Image description

We can check whether f1's prototype property and myobj's prototype property point to the same Prototype Object or not, by doing this,
Image description

as we saw above, that we can access function's prototype Object, in the similar way we can access the f1 Object itself by using a property called constructor. Constructor is a property on the Prototype Object that helps us to access the function itself.
Image description
Image description

Suppose if we want to know who created this object 'myobj', then we what we need to do is, simply use that constructor property to know this,
Image description

Thus we get the f1 function!
We can also create a new object by using this constructor property,
Image description

This simply implies, create an object myobj2 by using the same function/constructor who created the object myobj.
Up until now we saw what is this prototype property, what is this proto property and the constructor property, what all things do they point to and what are their uses!
However it is not recommended to use proto property very often!
The GlobalObject
In JRE,just as you have a global window object, in the similar way you also have a global functions. And one of those global functions is called Object. Name of the function is Object but the type is function.But since functions are Objects in JavaScript, this is also considered as an Object. I know this sounds a bit confusing!
Image description
Image description
Image description

The Prototype Object!
Lets create a function person and a new p1 object from it,
Image description
Image description

Looking at the Person's prototype object, makes us wonder,who created this Person's prototype object?
Well it was automatically created by 'new Object'! Person's prototype Object itself has proto property that points to the Global Object's Prototype.
Image description

Suppose if we try to access any property on p1 object, JS Engine will first try to find it inside p1. If it does not find it there, it will go up the prototype chain by using the proto property of p1.
p1.proto
If it even does not find it there also, it will again go one step up the prototype chain.
p1.proto.proto
And if it does not find it there as well, then it will return undefined!
Now there's one question, is this prototype chain never ending? Does base object's prototype have a proto property as well?
Well no, it does have proto property,but it points to null. Lets check this,
Image description
Image description

Prototypal Inheritance
Inheritance in JS is totally different than other programming languages. Its unlike classical inheritance.
Suppose if we have a Person function (as discussed above).And now we also have a Teacher function.Teacher will have all the properties of Person.
Initially Person's Prototype as well as Teacher's prototype(proto ) property both are pointing towards base Object's Prototype.
Image description

In this case, Teacher and person are not related. Teacher will not posses any property of Person. Well to overcome this, we can make the Teacher's prototype object(proto property) to point towards Person's prototype object.
Image description
Image description

Now teacher can access all properties of person.
This was all about Prototypes and Prototypal Inheritance!
Hope this article helps!
Happy learning!


Original Link: https://dev.to/shrushti23/prototypes-in-javascript-101a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To