简体   繁体   English

node.js的奇怪对象成员行为

[英]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 如果你然后执行console.log(obj)console.dir(obj) ,结果是

{ 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 在OS X Yosemite 10.10.1上, node --versionv0.10.33

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. 这些键总是字符串,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). 只是当你检查它们时,它们只会在需要时被引用(例如当它们包含保留字符或以数字开头时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM