Javascript 对象 - 允许以数字开头的键吗? [英] Javascript Object - Key beginning with number, allowed?

查看:43
本文介绍了Javascript 对象 - 允许以数字开头的键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这允许吗?

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)必须以字母开头.我目前有 第三版 并且 fireBug 标记了键(以数字开头)) 蓝色.

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.

推荐答案

如果您使用 [string] 访问键,则可以使用任何键,即使是带空格的键.所有这些都是有效的:

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屋!

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