Destructuring assignment

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Thu Aug 21 22:02:28 PDT 2008


Brendan Eich wrote:
> On Aug 21, 2008, at 8:23 PM, David-Sarah Hopwood wrote:
> 
>> Brendan Eich wrote:
>>> On Aug 21, 2008, at 6:47 PM, Mark S. Miller wrote:
>>>> On Thu, Aug 21, 2008 at 6:34 PM, Brendan Eich <brendan at mozilla.org>
>>>> wrote:
>>>>> js> [d,e,f] = foo()
>>>>> 1,2,3
>>>> Is this a simultaneous assignment of d,e,f, or does it declare
>>>> variables d,e,f?
>>>
>>> The first.
>>>
>>>> If the first, can you do a swap as
>>>>
>>>>     [d, e] = [e, d]
>>>
>>> Sure can (variable values as in previous mail): [...]
>>
>> So the rule is that the RHS is evaluated, and then the assignments are
>> done left-to-right, correct? And each array element (or property value
>> in the case of an object literal) on the left is syntactically a
>> LeftHandSideExpression?
> 
> Yes. In the binding forms, each element must be an identifier.
> 
>> (Sounds fine, I just want to confirm my understanding.)
> 
> http://wiki.ecmascript.org/doku.php?id=proposals:destructuring_assignment
> http://wiki.ecmascript.org/doku.php?id=discussion:destructuring_assignment

There is a comment at the end of the discussion page saying:

# We decided against object property shorthand in destructuring such as:
#
# { x, y, z } = { x: 1, y: 2, z: 3 }
#
# This would be nice and concise, but possibly so terse as to be confusing.
# Part of the justification of destructuring is that its syntax exactly
# mimics that of structuring.
# — Dave Herman 2006/10/20 12:24

and the syntax on the proposal page does not include this shorthand.

I don't have a particularly strong opinion on this (because it has
no semantic implications and the desugaring is trivial), but is the
shorthand proposed for ES-Harmony and/or ES3.1, or not?

Presumably, if it is proposed, then the syntax of 'field' using ES3
production names becomes:

   field ::= PropertyName ":" LeftHandSideExpression
           | Identifier

assuming that is is possible to mix the shorthand and longhand forms
for different fields.

-- 
David-Sarah Hopwood



More information about the Es-discuss mailing list