This figure again shows that every object has a prototype. Constructor function Foo also has its own
__proto__
which is Function.prototype, a
In JavaScript, a function can be used as a constructor. That means we can create objects out of them using the new keyword. Every constructor function comes with a built-in object chained with them. This built-in object is called a prototype. Instances of a constructor function use __proto__ to access the prototype property of its constructor function.
First we created a constructor: function Foo(){}
. To be clear, Foo is just another function. But we can create an object from it with the new keyword. That's why we call it the constructor function
Every function has a unique property which is called the prototype property. So, Constructor function Foo
has a prototype property which points to its prototype, which is Foo.prototype
(see image).
Constructor functions are themselves a function which is an instance of a system constructor called the [[Function]] constructor. So we can say that function Foo
is constructed by a [[Function]] constructor. So, __proto__
of our Foo function
will point to the prototype of its constructor, which is Function.prototype
.
Function.prototype
is itself is nothing but an object which is constructed from another system constructor called [[Object]]
. So, [[Object]]
is the constructor of Function.prototype
. So, we can say Function.prototype
is an instance of [[Object]]
. So __proto__
of Function.prototype
points to Object.prototype
.
Object.prototype
is the last man standing in the prototype chain. I mean it has not been constructed. It's already there in the system. So its __proto__
points to null
.
Now we come to instances of Foo
. When we create an instance using new Foo()
, it creates a new object which is an instance of Foo
. That means Foo
is the constructor of these instances. Here we created two instances (x and y). __proto__
of x and y thus points to Foo.prototype
.