Javascript 对象 - 允许以数字开头的键吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8698792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Object - Key beginning with number, allowed?
提问by user970727
Is this 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']
I ask that because somethings (for example an ID in HTML) must begin with a letter. I have at the moment the third versionand the fireBug marks the keys (beginning with a number) blue.
我这么问是因为某些东西(例如 HTML 中的 ID)必须以字母开头。我目前有第三个版本,fireBug 将键(以数字开头)标记为蓝色。
回答by taskinoor
You can use any key if you use [string]
to access the key, even key with space. All of these are valid:
如果您[string]
用来访问密钥,则可以使用任何密钥,即使是带空格的密钥。所有这些都是有效的:
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.
但是,如果您想使用点.
运算符访问密钥,则密钥必须是有效标识符,这意味着它们不能以数字开头或包含空格。
回答by abuduba
By dot operator you can get access to values from keys which not contains either space's or special characters, words starting from number( that is, those that can't be used as eg variable names) otherwise you can get any reffering to them like as keys in associative array.
通过点运算符,您可以从不包含空格或特殊字符的键中访问值,从数字开始的单词(即那些不能用作例如变量名的单词),否则您可以获得对它们的任何引用,例如关联数组中的键。
You can use as key as everything you want but remember the key will be a string representation of what you put in. Clarifying - will be called toString().
您可以使用任何您想要的键作为键,但请记住键将是您输入内容的字符串表示形式。澄清 - 将被称为 toString()。
Look:
看:
var myObj = {};
myObj[ 3 ] = "key is 3";
alert( myObj[ "3" ] ); //alerts "key is 3" because (3).toString() is "3"
//but an error will thrown when accessing by myObj.3
myObj[ {} ] = "key is {}"
alert( myObj["[object Object]"] ) // alerts "key is {}" because ({}).toString() is "[object Object]"
You can override toString()
method , eg:
您可以覆盖toString()
method ,例如:
Object.prototype.toString = function(){ return "object"}
a = {};
a[ {} ] = "whatever";
alert( a["object"] ); // alerts "whatever" because as now toString() returns "object" from each created object
回答by Niels
See this page: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables
请参阅此页面:https: //developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
JavaScript 标识符必须以字母、下划线 (_) 或美元符号 ($) 开头;后续字符也可以是数字 (0-9)。由于 JavaScript 区分大小写,因此字母包括字符“A”到“Z”(大写)和字符“a”到“z”(小写)。
You can use it that way, but you won't be able to access the data by using myObj.4a56546s6d
because starting an identifier with a number is not allowed.
您可以以这种方式使用它,但您将无法通过使用来访问数据,myObj.4a56546s6d
因为不允许以数字开头的标识符。