Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 285
Edward-Lombe Avatar asked Dec 07 '25 22:12

Edward-Lombe


1 Answers

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).

like image 174
Brad Avatar answered Dec 09 '25 12:12

Brad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!