Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Functions are objects in javascript, why the function.constructor points to Function instead of Object?

Tags:

javascript

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
like image 593
Yet A-beyene Avatar asked Oct 31 '22 08:10

Yet A-beyene


1 Answers

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.

like image 179
Oriol Avatar answered Nov 14 '22 23:11

Oriol