Reputation: 33
What attributes does the newly created property in prototye have that it cant be replaced see below::
Object.prototype.name="Maizere";
x=new Object();
console.log(x.name)//logs maizere
x.name="Pathak";
Instead of replacing the value of property with same name in prototye instead new property is created on object ,so i need to know everythig behind this confusing code
Upvotes: 1
Views: 49
Reputation: 324750
When you create the new object (before setting its name), you have something like this:
Object
> prototype
> name = "Maizerre"
So if you get its name, it looks first at its own properties, then at its prototype chain until it finds it.
At the end of the code, you have this:
Object
> name = "Pathak"
> prototype
> name = "Maizerre"
Now when you ask for the name, it finds the one that's the property of the object itself, and doesn't go looking along the prototype chain.
Upvotes: 3