$ node
a = {}
a['constructor']
[Function: Object]
Sorry if it is a known bug, I could not find it. Where I can read about?
@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!
Most helpful comment
@mbykov What is the expected behaviour for you here?
This seems to be right to me –
a.constructorshould be the built-inObjectfunction (becausea = {}is pretty much the same asa = new Object()).