Computed Property Name Shorthand Proposal

# Nicolas Bevacqua (9 years ago)

I'd expect the following to work, given that the prop expression evaluates to 'bar', and bar is in context.

var foo = 'bar'
var bar = 'ponyfoo'
var baz = { [foo] }
console.log(baz)
// <- { bar: 'ponyfoo' }

The following works

var foo = 'bar'
var bar = 'ponyfoo'
var baz = { [foo]: bar }
console.log(baz)
// <- { bar: 'ponyfoo' }

Sorry for not being quite spec-speak versed, this is my first message here. Nico

github bevacqua · blog ponyfoo.com · twitter twitter.com/nzgb · book bevacqua.io/buildfirst · career careers.stackoverflow.com/bevacqua ·about bevacqua.io

# Kevin Smith (9 years ago)

I'd expect the following to work, given that the prop expression evaluates to 'bar', and bar is in context.

var foo = 'bar' var bar = 'ponyfoo' var baz = { [foo] } console.log(baz) // <- { bar: 'ponyfoo' }

Hmmm... I'm not sure I would expect any such thing. It seems like you're proposing some kind of dynamic eval-ish variable binding lookup, which is probably going to be a no-go.

# Jeremy Martin (9 years ago)

I'm usually lousy at interpreting the spec (yet here I go!), but I think the behavior is explained here [1]:

PropertyDefinition[Yield] : IdentifierReference[?Yield] CoverInitializedName[?Yield] PropertyName[?Yield] : AssignmentExpression[In, ?Yield] MethodDefinition[?Yield]

PropertyName[Yield,GeneratorParameter] : LiteralPropertyName [+GeneratorParameter] ComputedPropertyName [~GeneratorParameter] ComputedPropertyName[?Yield]

LiteralPropertyName : IdentifierName StringLiteral NumericLiteral

ComputedPropertyName[Yield] : [ AssignmentExpression[In, ?Yield] ]

There are aspects to that grammar that I don't fully grok yet, but your { [foo] } example appears to be trying to combine the IdentifierReference form (which doesn't support an AssignmentExpression) and the PropertyName: AssignmentExpression form (which requires the AssignmentExpression).

In other words, all paths in that grammar that lead to a ComputedPropertyName being valid also require the right-hand AssignmentExpression.

[1] people.mozilla.org/~jorendorff/es6-draft.html#sec

# Alexander Jones (9 years ago)

That's exactly what this is. I appreciate the beauty in symmetry, but I think -1.

# Herby Vojčík (9 years ago)

Alexander Jones wrote:

That's exactly what this is. I appreciate the beauty in symmetry, but I think -1.

I second this.