什么是“键”的类型。在JavaScript? [英] What is the type of "keys" in JavaScript?
问题描述
当我失去焦点并开始怀疑一个愚蠢的问题时,我陷入了其中一个时刻:
I bumbed into one of those moments when I just lose the focus and start wondering on a silly question:
var a = {
b: "value"
}
'b'的类型是什么'而且我不是指价值的类型,而是标记为b的实际密钥?
What is the typeof 'b' and I don't mean the typeof "value", but the actual Key labeled as b?
背景:
当我拥有时,我开始怀疑这个创建一个字符串的键:
background: I started wondering about this when I had to create a key which is a string:
var a = {
"b": "value"
}
因为稍后它被引用为:
a["b"]
然后开始想知道原来的问题。
And then eneded up wondering the original question.
推荐答案
在对象字面意义上, b
是一个属性。属性是JavaScript中的字符串或符号 ,虽然在对象文字中定义属性名称时,您可以省略字符串分隔符。
In object literal terms, b
is a property. Properties are either strings or symbols in JavaScript, although when defining the property name inside an object literal you may omit the string delimiters.
for (key in a) {
alert(typeof key);
//-> "string" or "symbol"
}
这篇关于什么是“键”的类型。在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!