Object literal syntax for dynamic keys
2013/7/19 Jussi Kalliokoski <jussi.kalliokoski at gmail.com>
myObject = {
someKey: 'someValue',
[MY_KEY_ID]: 'my value'
};
I believe this is already proposed at harmony%3Aobject_literals#object_literal_computed_property_keys
These are computed properties and Allen has already added them to the latest ES6 draft.
ComputedPropertyName :
[ AssignmentExpression ]
Yes - but I believe computed property names are currently spec'ed to throw if the assignment expression evaluates to something other than a symbol
On Fri, Jul 19, 2013 at 9:10 AM, Kevin Smith <zenparsing at gmail.com> wrote:
Yes - but I believe computed property names are currently spec'ed to throw if the assignment expression evaluates to something other than a symbol
Can you point to where the current spec draft specifies this—I don't see it.
Sure - page 129 of the latest draft:
ComputedPropertyName : [ AssignmentExpression ]
- Let exprValue be the result of evaluating AssignmentExpression.
- Let propName be GetValue(exprValue).
- ReturnIfAbrupt(propName).
- If propName is not an exotic Symbol Object, then a. Throw a TypeError exception.
- Return propName.
right, this is listed in the agenda for the next TC39 meeting
If anybody can find in the TC39 meeting notes this was last discussed, that would be helpful. I think in was in 2011 or early 2012.
On Fri, Jul 19, 2013 at 11:32 AM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
If anybody can find in the TC39 meeting notes this was last discussed, that would be helpful. I think in was in 2011 or early 2012.
Potentially relevant:
Excellent, thanks!
I need to up my archive searching skills...
Anyway, I think it might be worth it to extend the syntax to support more than just symbols. For my cases, symbols will do the job, however for example for externally defined (e.g. config file) key names it would be quite handy to have calculated string keys as well.
How relevant is the object model reformation nowadays since we've decided that collections use get()/set() methods instead of accessors?
This is almost purely a syntactic sugar proposal, and if there's already a proposal like this being/been discussed, please point me that way, I couldn't find anything. Anyway, the use case is as follows:
You have keys that come, for example from a config file or are defined as constants, and you want to create objects with those keys. What you currently have to do is to use a mix of object notation syntax and assignment:
myObject = { someKey: 'someValue' }; myObject[MY_KEY_ID] = 'my value';
This is usually ok, but it's a bit confusing to read especially if the object is long and you try to find the definition inside the brackets but can't. Also, as far as I know,
"someKey"
andMY_KEY_ID
end up being defined differently; I doubt that the latter matters much, but what I have in mind would fix both things:myObject = { someKey: 'someValue', [MY_KEY_ID]: 'my value' };
So basically you could use brackets to signify that the key is the value of an expression inside the brackets.