Your understanding is correct that there are 3 objects and both abhik
and malay
inherit from the same Man
instance. But when you set new hands
properties on the malay
and abhik
objects, you give them their own hands
property and they are no longer inheriting the hands
property from the prototype Man
.
Illustration:
After you first create malay
and abhik
, here is a mock of your three objects:
father.prototype -> {hands: 2}
malay -> {name: ""} // Empty name property, NO hands property
abhik -> {name: ""} // Empty name property, NO hands property
When you check for the hands
property on malay
or abhik
, the interpreter will see that there is no such property and will check up the prototype chain and will find that their parent father.prototype
does have a hands
property, so the interpreter will report that value, which is 2
.
After you set the hands
properties, your objects look like this:
father.prototype -> {hands: 2}
malay -> {name: "", hands: 3} // Empty name property, OWN hands property
abhik -> {name: "", hands: 4} // Empty name property, OWN hands property
Now your objects all have their own hands
properties.
Resource: Here is a very well-written (but long) article about javascript inheritance: http://manuel.kiessling.net/2012/03/23/object-orientation-and-inheritance-in-javascript-a-comprehensive-explanation/