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
4
  • 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. Commented Feb 29, 2016 at 20:36
  • 3
    Functions are objects, not Objects. Commented Feb 29, 2016 at 20:42
  • 1
    See also this overview Commented Feb 29, 2016 at 20:59
  • why does new userInfo.prototype.constructor() creates object but new userInfo.constructor() doesn't ? Commented Jun 22, 2021 at 17:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Correction, obj.constructor prints out // Object
hm.. jsbin.com/namuyurime/edit?js,console check this, here hello.constructor is a function
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.