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?

4
  • 1
    Try 123..constructor, the first '.' is a decimal mark. {}.constructor works for me. Commented Jun 1, 2014 at 14:33
  • @Korikulum where? That's a syntax error in the chrome dev tools. It depends on where you're running it. Commented Jun 1, 2014 at 14:36
  • Suggest what? You haven't told us what you are trying to do. Commented Jun 1, 2014 at 14:36
  • @BenjaminGruenbaum Yes, you're right, I was doing the following: console.dir({}.constructor);, and that works. Commented Jun 1, 2014 at 14:39

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.