I'm trying to go back and get a better understanding of prototypal inheritance. I understand that an instance's __proto__
attribute points to the constructor function's prototype
object, but what does the constructor function's __proto__
attribute point to?
I had assumed that as the constructor function was itself an instance of Function, that it would point to the Function constructor's prototype
object, but the following shows it to be an empty function.
var Example = function(){
this.attribute = 'example';
}
var exampleInstance = new Example();
exampleInstance.__proto__ === Example.prototype // true
Example.__proto__ // function() {}
[Edit] Ovidiu Dolha has now confirmed my understanding so maybe this will be helpful to somebody.
Example.__proto__
will be the same as Function.prototype
, just as exampleInstance.__proto__
is the same as Example.prototype
This is because Example
is an instance of a Function.
Everything will eventually get to Object.prototype
which is the root in terms of prototypical inheritance.
Note that you should avoid using __proto__
if possible as it is considered deprecated. Instead use Object.getPrototypeOf()