Destructuring object outside of var declaration

Isiah Meadows isiahmeadows at gmail.com
Sun Nov 13 18:49:14 UTC 2016


Firefox likely has a parser bug (it should never throw a ReferenceError in
that situation).

As for the assignment, I wish that `{foo, bar} = extract()` was accepted as
a statement in more places.

On Sun, Nov 13, 2016, 13:28 Thaddee Tyl <thaddee.tyl at gmail.com> wrote:

> On Tue, Sep 17, 2013 at 4:27 PM, Brendan Eich <brendan at mozilla.com> wrote:
> >> Nathan Wall <mailto:nathan.wall at live.com>
> >> September 17, 2013 10:06 AM
> >> I'm wondering what the best syntax is for object destructuring outside
> of
> >> a var declaration.  For instance, the following works in Firefox
> Nightly and
> >> Traceur:
> >>
> >> […]
> >>
> >>     var a, b;
> >>     ({ a, b }) = foo;
> >>
> >> Is the above what people are expected to use (when they need to use
> >> destructuring outside of a var/let declaration or function arguments),
> or is
> >> there another form available?
> >
> > That's it. Lars Hansen originated destructuring for ES4 and implemented
> > array patterns in Futhark (Opera's engine of the time), but not object
> > patterns. His first proposal used
> >
> > &{a: x, b: y} = foo
> >
> > for just the reason you cite, but no one was keen on the ASI hazard
> > (previous line must end with a semicolon). We cut the & pretty soon in
> ES4's
> > evolution.
> >
> > I say use var/let/const, it's better style; or else suffer a parentheses
> > tax. This is pretty much a non-issue in practice, AFAICT.
>
> Have things evolved?
>
>     var foo, bar;
>     ({foo, bar}) = {foo: 2, bar: 3};
>
> is a "ReferenceError: invalid assignment left-hand side" in Firefox
> Nightly 51.0a2, and a "SyntaxError: Unexpected token (" in Google
> Chrome  54.0.2840.90.
>
>     var foo, bar;
>     {foo, bar} = {foo: 2, bar: 3};
>
> is a "SyntaxError: expected expression, got '='" in Firefox, and *it
> works in Google Chrome*.
>
>     var extract = () => ({foo:1, bar:2});
>     var foo, bar;
>     {foo, bar} = extract();  // SyntaxError: expected expression, got
> '=' in Firefox and Chrome
>     ({foo, bar}) = extract();  // ReferenceError: invalid assignment
> left-hand side in Firefox, SyntaxError: Unexpected token ( in Chrome
>
> I tried figuring out what the correct behaviour is from
>
> http://www.ecma-international.org/ecma-262/7.0/index.html#sec-destructuring-assignment
> ,
> but the rabbit hole goes deep, so I have no idea who to file a bug
> with.
>
> (Obviously, the "right" way to do it, `({foo, bar} = extract())`,
> works in both Firefox and Chrome. They, hum, could use some better
> error messages, too, to guide users towards that.)
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161113/1a0376e7/attachment.html>


More information about the es-discuss mailing list