Edward-Lombe
Edward-Lombe

Reputation: 57

Strange object member behaviour with node.js

Currently I'm trying to set the properties of an object using the square bracket notation. Code is as follows

var obj = {};
obj['c9c4d17a698ace65c80416112da3ff66e652ec013222f5b458a1dd4950580e77'] = 'one';
obj['8d207abeb95e36abfa2cdae6ac700e776c982ec64bcbfd501cb48fec55a13a77'] = 'two';

If you then do a console.log(obj) or console.dir(obj) the result is

{ c9c4d17a698ace65c80416112da3ff66e652ec013222f5b458a1dd4950580e77: 'one',
'8d207abeb95e36abfa2cdae6ac700e776c982ec64bcbfd501cb48fec55a13a77': 'two' }

What I want to know is why one property key is set as an unquoted literal and the other is set as a string. They are both being set in the same way. Am I falling victim to some escape sequence within the key?

node --version is v0.10.33 on OS X Yosemite 10.10.1

Upvotes: 3

Views: 31

Answers (1)

Brad
Brad

Reputation: 163313

Any time your object key starts with a number, it will appear as quoted when inspected in the console.

This doesn't affect any internal representation. These keys are always strings, as-assigned. It's just that when you inspect them, they'll only be quoted if they need to be (such as when they contain a reserved character or start with a number).

Upvotes: 2

Related Questions