+ 19
Why constructor property return this way?
(123).constructor //return function Number(){[native code]} but typeof(123) //return number I know difference between typeof operator and constructor property but don't understand why the output is like that?!!
7 ответов
+ 9
1. Constructor is function, (123) belongs to Number class, and (123).constructor is referring to the constructor function of Number.
https://www.sololearn.com/learn/JavaScript/2979/
2. Number is a class. You can use new keyword to create instance of class. Just adding parathesis is calling its constructor function to execute, but the memory is not allocated for a new instance of it.
+ 13
Thanks all of you ☺
+ 2
As Gordon said, it refers to the function Number, thus function Number(){[native code]}.
You can create a number in these ways, may be more but this is some of the basic ways:
new Number(123)
Number(123)
(123)
123
You can easily use operators like +, - and so on easily on all these like,
Number(10)+20 //output 30
Number sure is a class, therefore it has a constructor making the use of the new keyword possible, but it is not required by any means - same applies to the Date class etc.
And finally, leaving the Number function empty will return 0.
+ 1
as Gordon
+ 1
thank you all
+ 1
you can easily operators
+ 1
may be it was due to the difference in the member class