[英]javascript object property name as decimal notation
This is a more specific to this question , which only goes as far as discussing keys as numeric whole integers.这是一个更具体的问题,它只讨论作为数字整数的键。
In regards to 'numeric or string literal for the name of a property', I've tried this in the Chrome console var obj = { 2.15: 'foo' } console.log(obj[2.15])
and it works.关于“属性名称的数字或字符串文字”,我已经在 Chrome 控制台
var obj = { 2.15: 'foo' } console.log(obj[2.15])
尝试过这个,它可以工作。 I would not assume it standard across all browsers, especially older ones.我不会假设它在所有浏览器中都是标准的,尤其是旧浏览器。 However such a key notation as part of a default configuration provided by my users could be useful.
但是,作为我的用户提供的默认配置一部分的关键符号可能很有用。 I think using
Map
, though within standards ( 'Any value (both objects and primitive values) may be used as either a key or a value' ), could be intimidating.我认为使用
Map
,尽管在标准范围内( '任何值(对象和原始值)都可以用作键或值' ),可能会令人生畏。 If it works, why should it not be used?如果它有效,为什么不应该使用它?
The specification says: 规范说:
PropertyName : IdentifierName StringLiteral NumericLiteral
and shows that floats are fine in a NumericLiteral .并表明浮点数在NumericLiteral中很好。
It's standard.这是标准的。 Use it if you want to.
如果您愿意,请使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.