1

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?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Jyoti Puri
  • 1,326
  • 10
  • 17
  • 1
    Try `123..constructor`, the first '.' is a decimal mark. `{}.constructor` works for me. – Korikulum Jun 01 '14 at 14:33
  • @Korikulum where? That's a syntax error in the chrome dev tools. It [depends on where you're running it](http://stackoverflow.com/questions/17268468/why-is-nan-only-on-the-client-side-why-not-in-node-js/17269376#17269376). – Benjamin Gruenbaum Jun 01 '14 at 14:36
  • Suggest what? You haven't told us what you are trying to do. – JLRishe Jun 01 '14 at 14:36
  • @BenjaminGruenbaum Yes, you're right, I was doing the following: `console.dir({}.constructor);`, and that works. – Korikulum Jun 01 '14 at 14:39

1 Answers1

2

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
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504