+ 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?!!

13th Apr 2019, 9:33 AM
Ahad
Ahad - avatar
7 Antworten
+ 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.
13th Apr 2019, 5:19 PM
Gordon
Gordon - avatar
+ 13
Thanks all of you ☺
14th Apr 2019, 3:53 AM
Ahad
Ahad - avatar
+ 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.
13th Apr 2019, 9:04 PM
Ragey
Ragey - avatar
+ 1
as Gordon
15th Apr 2019, 6:06 PM
Rosalind Wells
Rosalind Wells - avatar
+ 1
thank you all
15th Apr 2019, 6:07 PM
Rosalind Wells
Rosalind Wells - avatar
+ 1
you can easily operators
15th Apr 2019, 6:08 PM
Rosalind Wells
Rosalind Wells - avatar
+ 1
may be it was due to the difference in the member class
17th Apr 2019, 8:20 AM
praveen