Node: 'constructor' bug

Created on 18 Feb 2020  Â·  5Comments  Â·  Source: nodejs/node

  • Version: v10.14.2
  • Platform: Linux asus 4.9.133-std-def-alt0.M80P.1 #1 SMP Mon Oct 15 16:46:38 UTC 2018 x86_64 GNU/Linux
  • Subsystem:

What steps will reproduce the bug?

$ node

a = {}
a['constructor']
[Function: Object]

How often does it reproduce? Is there a required condition?

What is the expected behavior?

What do you see instead?

Additional information

Sorry if it is a known bug, I could not find it. Where I can read about?

invalid

Most helpful comment

@mbykov What is the expected behaviour for you here?

> a.constructor === Object
true
> Object
[Function: Object]

This seems to be right to me – a.constructor should be the built-in Object function (because a = {} is pretty much the same as a = new Object()).

All 5 comments

@mbykov What is the expected behaviour for you here?

> a.constructor === Object
true
> Object
[Function: Object]

This seems to be right to me – a.constructor should be the built-in Object function (because a = {} is pretty much the same as a = new Object()).

Closing, not a bug. Please move it to nodejs/help if you have follow-up questions.

a = {}
a - is empty object, right?

Object.keys(a).length
0
a['any-string'] ? true : false
false
a['constructor'] ? true : false
true

Note, I am speaking about a['sring'], not about a.string, look:

if a[key] - do smth
else - do smth else

and you will get an error when key = 'constructor' - and I have got this error already several days ago.

That is a bug, definitely. Or you must write write about this behavior explicitly. What other strings should not be used in an empty object, except 'constructor'?

What other strings should not be used in an empty object, except 'constructor'?

All keys that exists on Object.prototype.

If you want a really "empty" object, you can create one with Object.create(null). This will set the object's prototype to null and so the object won't inherit any property.

Great, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

willnwhite picture willnwhite  Â·  3Comments

ksushilmaurya picture ksushilmaurya  Â·  3Comments

filipesilvaa picture filipesilvaa  Â·  3Comments