Reputation: 1346
I'm leaning JavaScript and I've read about the constructor
property.
I tried
[].constructor
false.constructor
'abc'.constructor
And they all worked. However to my surprise when trying:
123.constructor
{}.constructor
They did not, why is that?
Upvotes: 1
Views: 38
Reputation: 276436
This is a parse issue.
123.constructor
the engine experts a decimal after the 123
like 123.123
{}.constructor
the engine reads {}
as an empty block and then reads .
as the first thing in the outer block which is meaningless.Both of which can be fixed by wrapping these with ()
s
(123).constructor; // Number, note this 'boxes'
({}).constructor; // Object
Upvotes: 2