Explain JavaScript Prototypes
--
in this article, we will covering Prototype , prototype chaining , prototype inheritance.
Lets get on started.
to understand the Prototype let's create an array
let array = [“mango”, “orange”, “Banana”]
as we can show we create an array and when do .(dot) then we can access to many method and properties of that array.
Now We have thought like how do we get this. we didn’t defined it.
its not for array we do it for object, function , and variable we can get access many methods and properties .
in such scenario Prototype comes into the picture. javascript engine in the background attached your array, object etc. so some of objects which is actually what we call a prototype.
in this we can say this is object where all javascript putting all thes method which we get access to.
arry._proto_ has also its protoype which is
arry._proto_ _proto_ this also has a Prototype
arry._proto_ _proto_proto_. which will equals to null.
in the above attachment we can say its a Prototype chain.
-Prototypal inheritance.
we have two objects obj1 and obj2 .
let object1 = {
name: ‘satish’,
city: ‘gurgaon’,
x: function (){
console.log(this.name + “ “ + this.city);
}
}
let object2 = {
lastName: ‘sharma’
}
object2._proto_= object1
object2.city // gurgaon
Object2 will find the city in object2 but if it didn’t find it there it will now go to object1 and will take the city from there. So we can say it inherits from object1.
this is we can say prototypal inheritence
clap: if you enjoyed reading this article please clap
Comment: if you have a question/suggestion you’d like to ask