JavaScript 中“键”的类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3608246/
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
What is the type of "keys" in JavaScript?
提问by tunylund
I bumbed into one of those moments when I just lose the focus and start wondering on a silly question:
当我失去焦点并开始思考一个愚蠢的问题时,我偶然发现了其中一个时刻:
var a = {
b: "value"
}
What is the typeof 'b' and I don't mean the typeof "value", but the actual Key labeled as b?
'b' 的类型是什么,我指的不是“值”的类型,而是标记为 b 的实际键?
background: I started wondering about this when I had to create a key which is a string:
背景:当我不得不创建一个字符串键时,我开始想知道这个:
var a = {
"b": "value"
}
because at a later point it is referenced as:
因为在稍后它被引用为:
a["b"]
And then eneded up wondering the original question.
然后最终想知道原来的问题。
回答by Andy E
In object literal terms, bis a property. Properties are either strings or symbolsin JavaScript, although when defining the property name inside an object literal you may omit the string delimiters.
在对象字面意义上,b是一个属性。属性在 JavaScript中可以是字符串或符号,但在对象字面量中定义属性名称时,您可以省略字符串分隔符。
for (key in a) {
alert(typeof key);
//-> "string"
}
回答by Mathias Bynens
Property names are automatically coerced into a string. You can try this yourself by using a numeric literal as a property name.
属性名称会自动强制转换为字符串。您可以通过使用数字文字作为属性名称来自己尝试。
var object = {
.12e3: 'wut'
};
object[.12e3]; // 'wut'
object['.12e3']; // undefined
object['120']; // 'wut'
// Let's try another numeric literal:
object = {
12e34: 'heh'
};
object[12e34]; // 'heh'
object['12e34']; // undefined
object[1.2e35]; // 'heh'
object['1.2e35']; // undefined
object[1.2e+35]; // 'heh'
object['1.2e+35']; // 'heh'
For this reason, I'd recommend using only string literals for property names.
出于这个原因,我建议只对属性名称使用字符串文字。
From Unquoted property names / object keys in JavaScript, my write-up on the subject:
来自JavaScript 中的 Unquoted property names / object keys,我关于这个主题的文章:
Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.
[…]
Bracket notation can safely be used for all property names.
[…]
Dot notation can onlybe used when the property name is a valid identifier name.
如果属性名称是数字文字或有效的标识符名称,则只能省略引号。
[…]
括号表示法可以安全地用于所有属性名称。
[…]
点符号只能在属性名称是有效标识符名称时使用。
I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.
我还制作了一个工具,它会告诉您是否可以在不使用引号和/或点符号的情况下使用任何给定的属性名称。在mothereff.in/js-properties尝试一下。
回答by aularon
b is a string, it's just a shorthand syntax, so you write
b 是一个字符串,它只是一个速记语法,所以你写
var a = {
b: "value"
}
instead of
代替
var a = {
"b": "value"
}
回答by Daniel Vassallo
Keep in mind that JavaScript objects are hash tables and the keys are just strings. You may omit the quotes around property names during declaration, but if you use reserved words for property names or any other name that happens to be an invalid identifier, such as starting with a digit, or containing spaces, you would have to wrap the property names in quotes:
请记住,JavaScript 对象是哈希表,而键只是字符串。您可以在声明期间省略属性名称周围的引号,但是如果您对属性名称或任何其他碰巧是无效标识符的名称使用保留字,例如以数字开头或包含空格,则必须包装该属性引号中的名称:
var a = {
"1b": "value",
"b and c": "value",
"+12345": "value"
};
Also note that you can reference the properties of objects using the dot notation or the subscript notation regardless of whether quotes where used when they were declared. However, if you use property names that would be invalid identifiers, such as the ones in the above example, you are forced to use the subscript notation:
另请注意,您可以使用点表示法或下标表示法引用对象的属性,而不管在声明它们时是否使用了引号。但是,如果您使用无效标识符的属性名称,例如上面示例中的那些,您将被迫使用下标符号:
a.1b // invalid (dot notation)
a["b and c"]; // valid (subscript notation)
回答by Shubham Chaurasia
var a = {$ : 'hello', 2123 : 'number'};
for(var key in a) {
console.log(typeof key)
}
Keys in javascript objects can be strings and symbols. symbol is a primitive data type in javascript.
javascript 对象中的键可以是字符串和符号。符号是javascript中的一种原始数据类型。


