JavaScript对象 - 用数字键开始,允许? [英] Javascript Object - Key beginning with number, allowed?
问题描述
这可以吗?
myObj = {};
myObj['4a56546s6d']
或密钥必须以字母开头,如:
Or the key must start with a letter like:
myObj = {};
myObj['x4a56546s6d']
我可以混用都喜欢:
Can I mix both like:
myObj = {};
myObj['x4a56546s6d']
myObj['4a56546s6d']
我问这个,因为有些事情(例如HTML中的ID)必须以字母开头。我此刻的第三个版本和萤火虫标志着键蓝(以数字开头)。
I ask that because somethings (for example an ID in HTML) must begin with a letter. I have at the moment the third version and the fireBug marks the keys (beginning with a number) blue.
推荐答案
可以,如果您使用使用任何键 [字符串]
来访问密钥,甚至与空格键。所有这些都是有效的:
You can use any key if you use [string]
to access the key, even key with space. All of these are valid:
myObj['key with space']
myObj['12345']
但是,如果你想用点。
操作员访问密钥则该密钥必须是有效的标识符,这意味着他们不能以数字开头或包含空格。
But if you want to use dot .
operator to access the key then the key must be a valid identifier which means they can not begin with a number or contain space.
这篇关于JavaScript对象 - 用数字键开始,允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!