The constructor property value of any object is inherited from its prototype object's constructor
property value.
The prototype of an object is set to the prototype
property value of the function (object) used to construct it, at the time it was created.
Because function objects are created using the global Function
constructor function, they inherit Function
as their constructor
property:
function Foo(){}; // Foo is constructed by global object "Function".
Foo.constructor; // Function, inherited from Function.prototype.constructor property
Note the prototype object of Function
is set to Function.prototype
in native code:
Object.getPrototypeOf(Function) === Function.prototype // is true.
This has the effect that any properties added to Function.prototype are inherited by Function. This is desirable because Function
is, of course, a function. As an implicit side effect however it also results in native code Function
being returned as the constructor of itself.
Also:
Function.prototype
is an instance of Object so function objects inherit from Object.prototype as well.
Use of the "constructor" property in code has no guarantee of safety. While the prototype
property and its constructor
property may not be changed for Function
, neither is write protected for function objects in general.