ES5 quote that says it should not work
Note: rules have changed for ES6: https://stackoverflow.com/a/2274327/895245
Spec: http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5
PropertyName :
- IdentifierName
- StringLiteral
- NumericLiteral
[...]
The production PropertyName : IdentifierName is evaluated as follows:
- Return the String value containing the same sequence of characters as the IdentifierName.
The production PropertyName : StringLiteral is evaluated as follows:
- Return the SV [String value] of the StringLiteral.
The production PropertyName : NumericLiteral is evaluated as follows:
- Let nbr be the result of forming the value of the NumericLiteral.
- Return ToString(nbr).
This means that:
{ theTop : 10 }
is the exact same as { 'theTop' : 10 }
The PropertyName
theTop
is an IdentifierName
, so it gets converted to the 'theTop'
string value, which is the string value of 'theTop'
.
It is not possible to write object initializers (literals) with variable keys.
The only three options are IdentifierName
(expands to string literal), StringLiteral
, and NumericLiteral
(also expands to a string).