使用带点表示法的整数键访问javascript对象中的属性 [英] Using integer keys with dot notation to access property in javascript objects
问题描述
为什么我们不能在点表达式中使用整数键来访问属性值?
var obj = {1:一个",两个:"2"}console.log(obj.1)//错误console.log(obj.two)
对于 您可以使用括号表示法在这种情况下 规范:财产访问者 > Why can't we use integer keys in dot expression to access property values ?
In case of dot notation to access a value, the property key must be a valid identifier In this code, property must be a valid JavaScript identifier, i.e. a
sequence of alphanumerical characters, also including the underscore
("_") and dollar sign ("$"), that cannot start with a number. For
example, object.$1 is valid, while object.1 is not. You can use bracket notation in this case Spec: Property Accessors 这篇关于使用带点表示法的整数键访问javascript对象中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
obj ['1']
var obj = {1: 'one', two: '2'}
console.log(obj.1) // error
console.log(obj.two)
obj['1']