Foo.prototype
has a property constructor
, which is defined as Foo.prototype.constructor = Foo;
and that's why the console shows it like that
That's the consequence of Foo.prototype
having a constructor
property. Each prototype of a constructor function has a constructor
property which points back to that function, e.g.
Foo.prototype.constructor = Foo;
Now, it looks like in order to determine which constructor function an object is an instance of, Chrome's console looks at the constructor
property of an object and then assumes that the object is an instance of that function.
This works in almost all cases, but not for prototype objects, because they have explicitly set a constructor
property.
Example:
> function Foo() {}
undefined
> Foo.prototype.constructor = function xyz() {}
function xyz() {}
> Foo.prototype
xyz {}
Another, simpler example:
> var obj = {};
> obj
Object {}
> obj.constructor = function xyz() {};
function xyz() {}
> obj
xyz {}
As you can see, the console is really just looking at the constructor
property of an object (which is usually inherited) and then prints the function name of the function assigned to it.
Foo.prototype.constructor
is just a normal property which is assigned when a function is created.
It's purpose is that actual instances of Foo
(created with new Foo
) have a constructor property which points to Foo.
Since instances inherit all properties from the prototype.