模板字符串作为对象属性名

时间:2021-08-25 19:01:44

Why does JavaScript not allow a template string as an object property key? For example, when I input:

为什么JavaScript不允许模板字符串作为对象属性键?例如,当我输入:

foo = {`bar`: 'baz'}

into the NodeJS REPL, it throws a SyntaxError with "Unexpected template string" with a long stack trace. Property values are fine, however, which is not as unexpected. Similar errors happen in the browser, for example, Firebug throws a SyntaxError with "invalid property id".

在NodeJS REPL中,它使用带有长堆栈跟踪的“意外模板字符串”抛出SyntaxError。但是,属性值是好的,这并不是没有预料到的。类似的错误发生在浏览器中,例如,Firebug抛出带有“无效属性id”的SyntaxError。

Template strings are allowed in "computed property names". For instance, this compiles perfectly fine in all browsers that support the syntax:

在“计算属性名”中允许使用模板字符串。例如,在支持语法的所有浏览器中,这都是完美的。

var foo = {
    [`bar` + 1]: `baz`
};

and creates the object {"bar1": "baz"}.

并创建对象{"bar1": "baz"}。

Why are template strings not allowed as literal object keys? Is it for performance reasons? Template strings must be compiled, possibly at runtime (correct me if I'm wrong), which means every time it encounters this object, the interpreter will have to compute the object name. Factoring in things like "cooked" template strings, this seems like it could get slow, although we have had getters and setters since ES5. Firefox does not mention this as an error, which is why I found it unexpected. Will the syntax be allowed sometime in the future?

为什么不允许模板字符串作为文本对象键?是因为性能原因吗?模板字符串必须被编译,可能在运行时(如果我错了,请纠正我),这意味着每次遇到这个对象时,解释器都必须计算对象名。考虑到“煮熟的”模板字符串等内容,这似乎会变得很慢,尽管自ES5以来我们已经有了getter和setter。Firefox没有提到这是一个错误,这就是为什么我发现它是意想不到的。将来某个时候允许使用这种语法吗?

1 个解决方案

#1


33  

Why are template strings not allowed as literal object keys?

为什么不允许模板字符串作为文本对象键?

Template strings are expressions, not literals1. You can only use string literals (and identifiers) for property names, for everything else - that is not known to be static - you need a computed property name.

模板字符串是表达式,而不是文字s1。对于属性名,您只能使用字符串文字(和标识符),对于其他所有不知道是静态的东西,您需要计算属性名。

Is it for performance reasons?

是因为性能原因吗?

No, that's unlikely. It's to ease parsing, and makes it easy to distinguish constant (statically known) property names from dynamically computed ones.

不,那是不可能的。它简化了解析,使常量(静态已知的)属性名与动态计算的属性名更容易区分。

And mostly, it's a feature that no one needs. It doesn't simplify or shorten anything, and what you would achieve with it is already possible.

大多数情况下,这是一个没有人需要的功能。它不会简化或缩短任何事情,你用它来实现的目标已经是可能的。

Will the syntax be allowed sometime in the future?

将来某个时候允许使用这种语法吗?

Nope.

不。

1: Even when they're called "template literals", technically they aren't literals. And: templates don't even need to be strings, they can evaluate to anything.

1:即使他们被称为“模板文字”,技术上他们也不是文字。而且:模板甚至不需要是字符串,它们可以计算任何值。

#1


33  

Why are template strings not allowed as literal object keys?

为什么不允许模板字符串作为文本对象键?

Template strings are expressions, not literals1. You can only use string literals (and identifiers) for property names, for everything else - that is not known to be static - you need a computed property name.

模板字符串是表达式,而不是文字s1。对于属性名,您只能使用字符串文字(和标识符),对于其他所有不知道是静态的东西,您需要计算属性名。

Is it for performance reasons?

是因为性能原因吗?

No, that's unlikely. It's to ease parsing, and makes it easy to distinguish constant (statically known) property names from dynamically computed ones.

不,那是不可能的。它简化了解析,使常量(静态已知的)属性名与动态计算的属性名更容易区分。

And mostly, it's a feature that no one needs. It doesn't simplify or shorten anything, and what you would achieve with it is already possible.

大多数情况下,这是一个没有人需要的功能。它不会简化或缩短任何事情,你用它来实现的目标已经是可能的。

Will the syntax be allowed sometime in the future?

将来某个时候允许使用这种语法吗?

Nope.

不。

1: Even when they're called "template literals", technically they aren't literals. And: templates don't even need to be strings, they can evaluate to anything.

1:即使他们被称为“模板文字”,技术上他们也不是文字。而且:模板甚至不需要是字符串,它们可以计算任何值。