Javascript对象中的键只能是字符串? [英] Keys in Javascript objects can only be strings?

查看:207
本文介绍了Javascript对象中的键只能是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jshashtable状态:

jshashtable states:


JavaScript的内置对象使用
提供哈希表功能$ b $的方括号表示法b属性,只要你的密钥是
字符串或数字:

JavaScript's built-in objects do provide hashtable functionality using the square brackets notation for properties, provided your keys are strings or numbers:

据我所知,密钥只是字符串,(因为无论如何,数字都被强制转换为字符串。我只想检查并确保上述内容是错误的(因为密钥不能是数字)。

From what I know, keys are only strings, (since numbers are coerced into strings anyway). I just want to check and be sure that what is stated above is false (since keys can't be numbers).

ECMA标准是否说明了这一点..

Did ECMA standard stated anything about this..

或者是特定于浏览器的实现?

Or is the implementation browser-specific?

推荐答案


JavaScript的内置对象确实提供哈希表功能,使用
属性的方括号表示法,前提是你的键是
字符串或数字

JavaScript's built-in objects do provide hashtable functionality using the square brackets notation for properties, provided your keys are strings or numbers

这似乎不正确 - 对象键 始终是字符串可以是字符串或(因为ECMAScript 2015,又名ECMA-262 ed 6)符号。但这是方括号属性访问的另一个主题。

That seems to be incorrect - object keys are always strings may be strings or (since ECMAScript 2015, aka ECMA-262 ed 6) symbols. But that is a different topic to square bracket property access.

参见ECMA-262 ed3§11.2.1(另请参阅 ECMAScript 2017(草稿)。):

See ECMA-262 ed 3 § 11.2.1 (Please also see ECMAScript 2017 (draft).):


通过名称访问属性,使用点符号:

Properties are accessed by name, using either the dot notation:

MemberExpression。 IdentifierName

CallExpression。 IdentifierName

或括号表示法:

MemberExpression [Expression]

CallExpression [Expression]

点符号由以下内容解释句法转换:

The dot notation is explained by the following syntactic conversion:

MemberExpression。 IdentifierName

的行为与

MemberExpression [< identifier-名称字符串> ]

同样

CallExpression。 IdentifierName

的行为与

CallExpression [< identifier-名称字符串> ]

其中< identifier-name-string> 是一个包含
相同字符序列的字符串文字在处理Unicode转义后,
序列作为IdentifierName。

where <identifier-name-string> is a string literal containing the same sequence of characters after processing of Unicode escape sequences as the IdentifierName.

所以当使用点表示法时,点后面的位必须适合IdentifierName的标准。但是当使用方括号时,会提供一个表达式,它被评估并解析为一个字符串。

So when using dot notation, the bit after the dot must fit the criteria for an IdentifierName. But when using square brackets, an expression is provided that is evaluated and resolved to a string.

简而言之,提供了方括号表示法,以便可以使用表达式访问属性,例如

Briefly, square bracket notation is provided so that properties can be accessed using an expression, e.g.

var y = {};
var x = 'foo';
y[x] = 'foo value';

在上面,提供了 x 方括号因此被评估,返回字符串'foo'。由于此属性在 y 上尚不存在,因此会添加该属性。然后为 y foo 属性分配值'foo value'。

In the above, x is provided in square brackets so it is evaluated, returning the string 'foo'. Since this property doesn't exist on y yet, it is added. The foo property of y is then assigned a value of 'foo value'.

一般而言,方括号中的表达式被计算,并且其 toString()方法被调用。它是用作属性名称的值。

In general terms, the expression in the square brackets is evaluated and its toString() method called. It is that value that is used as the property name.

在点属性访问方法中,不评估标识符,因此:

In the dot property access method, the identifier is not evaluated, so:

y.bar = 'bar value';

使用值<创建属性 bar code> bar值。

creates a property bar with a value bar value.

如果你想创建一个数字属性,那么:

If you want to create a numeric property, then:

y[5] = 5;

将评估 5 ,看到它不是string,call(或多或少) Number(5).toString()返回字符串 5 ,这是用于属性名称。然后为其分配值 5 ,这是一个数字。

will evaluate 5, see it's not a string, call (more or less) Number(5).toString() which returns the string 5, which is used for the property name. It is then assigned the value 5, which is a number.

这个答案是在ECMAScript ed3是最新的时候编写的,但事情已经发生了变化。请参阅后面的参考资料和 MDN

This answer was written when ECMAScript ed3 was current, however things have moved on. Please see later references and MDN.

这篇关于Javascript对象中的键只能是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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