“钥匙"的类型是什么?在 JavaScript 中? [英] What is the type of "keys" in JavaScript?

查看:26
本文介绍了“钥匙"的类型是什么?在 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' 的 typeof 是什么,我指的不是value"的 typeof,而是标记为 b 的实际 Key?

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 中的 symbols,尽管在对象字面量中定义属性名称时,您可以省略字符串分隔符.

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"
}

这篇关于“钥匙"的类型是什么?在 JavaScript 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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