如何以数字开头访问对象属性(SyntaxError:Unexpected identifier) [英] How to access object property beginning with a number (SyntaxError: Unexpected identifier)

查看:108
本文介绍了如何以数字开头访问对象属性(SyntaxError:Unexpected identifier)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个对象中有一个对象,我试图获取该值,但它总是返回意外的标识符。

I have an object within another object, which im trying to get the value but it always returns "unexpected identifier".

snow: Object {3h: 1.3}

console.log(data.snow.3h) //returns Uncaught SyntaxError: Unexpected identifier

console.log(data.snow) //returns Object {3h: 1.3}

那么我怎样才能得到3h的值?

So how can i get the value of 3h ?

推荐答案

data.snow['3h'];

使用点表示法访问的属性不能以数字开头。

Properties accessed with dot notation can't begin with a number.

snow:对象{3h:1.3} 可以重构为 snow:{3h:1.3} 。键入 Object 是多余的。

snow: Object {3h: 1.3} could be refactored to snow: {3h: 1.3}. It is redundant to type Object.

此外,如果您将属性名称包装在引号中,则可以使用奇异的属性名称如:

Also, if you wrap your property names in quotes, you can use bizarre property names like:

var myObj = {
  '^': 'foo'
};
console.log(myObj['^']);

但是,我通常坚持使用点符号来访问更多标准名称。

but, I generally stick to more standard names that I can access with dot notation.

这篇关于如何以数字开头访问对象属性(SyntaxError:Unexpected identifier)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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