Why does JavaScript not allow a template string as an object property key? For example, when I input:
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".
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"}
.
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?