Reputation: 11791
All, I always confused by the __proto__
and prototype
properties of the object, Especially In the prototype inheritance chain, let's say there is an object named test
, and the code is below.
test.add();//try to call a method name add.
in my understanding ,I draw a diagram to demo the flow of searching . But I am not sure if it is right .please help to review it .thanks.
Corrected it based on the Minko Gechev's answer.
Upvotes: 1
Views: 144
Reputation: 25682
Only functions have prototype
property but any object has __proto__
property.
Usually it's not a good idea to change the __proto__
property explicit better use something like Object.create
.
Here is an example:
function Person() {
}
var proto = { bar: 'baz' };
Person.prototype = proto;
In this way any object which you create with the constructor function Person
will have __proto__
property referencing to proto
As conclusion we can say that:
The prototype property is used only for the constructor function it sets the __proto__
property of all objects which will be created with this constructor function. When the interpreter is looking for property of a given object foo
it firstly use the object foo
after that foo.__proto__
after that foo.__proto__.__proto__
and so on while it find the property or not.
Upvotes: 3