使用带点表示法的整数键访问javascript对象中的属性 [英] Using integer keys with dot notation to access property in javascript objects

查看:43
本文介绍了使用带点表示法的整数键访问javascript对象中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能在点表达式中使用整数键来访问属性值?

  var obj = {1:一个",两个:"2"}console.log(obj.1)//错误console.log(obj.two) 

解决方案

对于

您可以使用括号表示法在这种情况下

  obj ['1'] 

规范:财产访问者

Why can't we use integer keys in dot expression to access property values ?

var obj = {1: 'one', two: '2'}
console.log(obj.1) // error
console.log(obj.two)

解决方案

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

obj['1']

Spec: Property Accessors

这篇关于使用带点表示法的整数键访问javascript对象中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆