Destructuring syntax infelicity viz. identifier vs. property name

Dominic Cooney dominicc at google.com
Thu Oct 21 22:58:06 PDT 2010


The proposed syntax for destructuring objects at <
http://wiki.ecmascript.org/doku.php?id=harmony:destructuring> is:

Pattern ::= "{" (Field ("," Field)* ","?)? "}"
        (array destructuring elided)
Element ::= Pattern | LValue
Field ::= Identifier (":" Element)?
LValue ::= <any lvalue expression allowed in a normal assignment expression>

This misses the opportunity for some parallelism with object literals. For
example, I can construct {' 0blah': x} with an object literal, but I can't
access the ' 0blah' with object destructuring.

Maybe Field ::= Identifier (":" Element)? could be changed to:

Field ::= PropertyName (":" Element)?

(using ECMA-262's PropertyName) to permit {' 0blah': x} = ...

This also allows seeming nonsense like {' 0blah'}, but I'm not sure that is
so bad since bindings in *objects* can have names that aren't identifiers,
so why not in scopes in general, too? Although destructuring could no longer
be implemented via desugaring because there's no way to make a binding with
an arbitrary name in the current scope.

So to preserve desugarability you could spell out the alternatives of
PropertyName in Field, to require elements with strings and numbers:

Field ::= Identifier (":" Element)?
    | StringLiteral ":" Element
    | NumericLiteral ":" Element

Dominic
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101022/97a683c4/attachment.html>


More information about the es-discuss mailing list