about 7 years ago
Originally, I thought there is only one prototype
property per function until I read about __proto__
. Take a look here:
function Person(){
this.name = 'John';
}
Person.prototype // Object{}
// > constructor: function Person()
// > __proto__: Object
Person.__proto__ // function() {}
I was totally confused until I read this stackoverflow post. First of all, this graph helps to clarify things a bit:
The accept answer nails it:
__proto__
is the actual object that is used in the lookup chain to resolve methods, etc.prototype
is the object that is used to build__proto__
when you create an object withnew
:
( new Foo ).__proto__ === Foo.prototype
( new Foo ).prototype === undefined
As one of the users commented:
Ah! So prototype is not available on the instances themselves, but only on the constructor functions.
Here is a table to summarize the idea, considering the code blow:
var obj = {}
function Person(){
}
var p1 = new Person()
__proto__ | prototype | |
---|---|---|
obj | Object | undefined |
Person() | Function |
Object { constructor: function Person() __proto__: Object } |
p1 |
Object { constructor: function Person() __proto__: Object } |
undefined |
Note: obj.__proto__
is the Object.prototype and Person._proto__
is Function.prototype