Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is __proto__ for Number, Date, Boolean, Array

Tags:

javascript

I draw a picture based on prototype chain relationship.

but wonder where the __proto__ of Number, Date, Boolean, Array point to.

Your comment welcome

enter image description here

like image 741
arachide Avatar asked Jun 19 '26 10:06

arachide


2 Answers

When in doubt, you can check the spec:

15.7.3 Properties of the Number Constructor

The value of the [[Prototype]] internal property of the Number constructor is the Function prototype object (15.3.4).

15.9.4 Properties of the Date Constructor

The value of the [[Prototype]] internal property of the Date constructor is the Function prototype object (15.3.4).

15.6.3 Properties of the Boolean Constructor

The value of the [[Prototype]] internal property of the Boolean constructor is the Function prototype object (15.3.4).

15.4.3 Properties of the Array Constructor

The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object (15.3.4).

And the reasoning is that those objects are functions/constructors. So you may want to use function methods on them.

For example, a (bad) way to convert array-like objects to arrays:

Array.apply(void 0, {0: 'a', 1: 'b', 2: 'c', length: 3}) // ["a", "b", "c"]
like image 74
Oriol Avatar answered Jun 21 '26 23:06

Oriol


Since all of those are functions (typeof Date = typeof Number = "function") it points to Function.prototype.

It's easy to verify by simply checking:

Date.__proto__ === Function.prototype; // true

This is the case because they are functions (you call them as functions), they contain all the things functions need to do (such as .call .bind and .apply)

like image 38
Benjamin Gruenbaum Avatar answered Jun 22 '26 01:06

Benjamin Gruenbaum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!