5

When creating the following function from the window object like this,

         function userInfo() {};

how come the userInfo.constructor displays Function instead of Object since functions are objects?

It even display Function instead of Object when using the following,

         Function.constructor
Yet A-beyene
  • 383
  • 1
  • 2
  • 8
  • 1
    Arrays are objects with the Array constructor. Date instances are objects with the Date constructor. Something can be an object and not be constructed by the Object constructor. – Pointy Feb 29 '16 at 20:36
  • 3
    `Function`s are objects, not `Object`s. – Bergi Feb 29 '16 at 20:42
  • 1
    See also [this overview](http://stackoverflow.com/q/7018023/1048572) – Bergi Feb 29 '16 at 20:59
  • why does `new userInfo.prototype.constructor()` creates object but `new userInfo.constructor()` doesn't ? – Rishabh Agrawal Jun 22 '21 at 17:49

3 Answers3

2

userInfo.constructor is Function because:

  1. userInfo has no own constructor property.
  2. The value of userInfo's [[Prototype]] internal slot is Function.prototype.
  3. Function.prototype has an own constructor property, whose value is Function.

Function.constructor is Function too because of the same reason:

  1. Function has no own constructor property.
  2. The value of Function's [[Prototype]] internal slot is Function.prototype.
  3. Function.prototype has an own constructor property, whose value is Function.

That is, Function instances (like userInfo or Function itself) inherit a constructor property from Function.prototype, which can be used to know they are instances of Function.

Oriol
  • 274,082
  • 63
  • 437
  • 513
0

Functions are high level object. Actually, function is an object. That's why you can assign your functions any values like userInfo.anotherFunction = function() { return 'something'}; and then call userInfo.anotherFunction();. That's why you have both

var obj = {};
function myFunc() {}

console.log(obj.constructor)
console.log(myFunc.constructor)

These both will print functions as constructors. Simply, if every object has .constructor property, and any function in javascript is itself object you surely will have .constructor property on any function.

Also Function is a constructor of any function. Any constructor is a function that's why Function also has .constructor property

Check this link http://ejohn.org/apps/learn/#16

aprok
  • 1,147
  • 11
  • 25
0

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.

traktor
  • 17,588
  • 4
  • 32
  • 53