Jyoti Puri
Jyoti Puri

Reputation: 1346

Constructor property of Java Script types

I'm leaning JavaScript and I've read about the constructor property.

I tried

  1. [].constructor
  2. false.constructor
  3. 'abc'.constructor

And they all worked. However to my surprise when trying:

  1. 123.constructor
  2. {}.constructor

They did not, why is that?

Upvotes: 1

Views: 38

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276436

This is a parse issue.

  • When you do 123.constructor the engine experts a decimal after the 123 like 123.123
  • When you do {}.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

Related Questions