“密钥”的类型是什么?在 JavaScript 中?

发布于 2024-09-16 12:07:21 字数 291 浏览 4 评论 0原文

当我失去焦点并开始思考一个愚蠢的问题时,我遇到了这样的时刻:

var a = {
  b: "value"
}

“b”的类型是什么,我的意思不是“值”的类型,而是标记为 b 的实际键?

背景: 当我必须创建一个字符串键时,我开始想知道这一点:

var a = {
  "b": "value"
}

因为后来它被引用为:

a["b"]

然后最终想知道原来的问题。

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?

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 ended up wondering about the original question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只为一人 2024-09-23 12:07:21

用对象字面量来说,b 是一个属性。属性可以是 JavaScript 中的字符串或 符号,尽管在对象文字中定义属性名称时,您可以省略字符串分隔符。

for (key in a) {
    alert(typeof key);
    //-> "string"
}

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"
}
看轻我的陪伴 2024-09-23 12:07:21

属性名称会自动强制转换为字符串。您可以通过使用数字文字作为属性名称来尝试此操作。

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'

因此,我建议仅使用字符串文字作为属性名称。

来自 JavaScript 中不带引号的属性名称/对象键,我关于这个主题的文章:

仅当属性名称是数字文字或有效的标识符名称时,才可以省略引号.

[…]

括号表示法可以安全地用于所有属性名称。

[…]

仅当属性名称是有效的标识符名称时,才可以使用点表示法。

我还制作了一个工具,它会告诉您是否可以使用任何给定的属性名称而不使用引号和/或点符号。请访问 mothereff.in/js-properties 尝试一下。

Screenshot

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:

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 only be 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.

Screenshot

剪不断理还乱 2024-09-23 12:07:21

b 是一个字符串,它只是一个简写语法,所以你写

var a = {
    b: "value"
}

而不是

var a = {
  "b": "value"
}

b is a string, it's just a shorthand syntax, so you write

var a = {
    b: "value"
}

instead of

var a = {
  "b": "value"
}
笑叹一世浮沉 2024-09-23 12:07:21

请记住,JavaScript 对象是哈希表,键只是字符串。您可以在声明期间省略属性名称周围的引号,但如果您对属性名称或任何其他恰好是无效标识符的名称使用保留字,例如以数字开头或包含空格,则必须将属性包装起来引号中的名称:

var a = {
  "1b":       "value",
  "b and c":  "value",
  "+12345":   "value"
};

另请注意,您可以使用点符号或下标符号引用对象的属性,无论声明时是否使用引号。但是,如果您使用的属性名称是无效标识符,例如上面示例中的属性名称,则必须使用下标表示法:

a.1b             // invalid (dot notation)
a["b and c"];    // valid   (subscript notation)

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:

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 were 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)
染墨丶若流云 2024-09-23 12:07:21
var a = {$ : 'hello', 2123 : 'number'};
for(var key in a) {
  console.log(typeof key)
}

javascript 对象中的键可以是字符串和符号。符号是JavaScript中的一种原始数据类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文