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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33194138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:45:50  来源:igfitidea点击:

Template String As Object Property Name

javascriptnode.jsobject-literaltemplate-stringstemplate-literals

提问by trysis

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 SyntaxErrorwith "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 SyntaxErrorwith "invalid property id".

进入 NodeJS REPL,它会抛出一个SyntaxError带有“意外模板字符串”的带有长堆栈跟踪的字符串。然而,属性值很好,这并不出人意料。类似的错误发生在浏览器中,例如,Firebug 会抛出一个SyntaxError“无效的属性 ID”。

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 没有将此作为错误提及,这就是我发现它出乎意料的原因。将来某个时候会允许使用该语法吗?

采纳答案by Bergi

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.

模板字符串是表达式,而不是文字1。您只能将字符串文字(和标识符)用于属性名称,对于其他所有内容(不知道是静态的),您需要一个计算属性名称。

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:即使它们被称为“模板文字”,从技术上讲,它们也不是文字。并且:模板甚至不需要是字符串,它们可以评估任何东西。