nested destructuring of undefined and default parameters

Aaron Frost aaronfrost at gmail.com
Tue Feb 5 07:40:52 PST 2013


my apologies for not reading that ahead of time.

I just took the time to read it and must say that I really like this. The
syntax is easy to read and offers flexibility. Kudos to all who contrib'd
to this.

AF


On Tue, Feb 5, 2013 at 7:50 AM, Andreas Rossberg <rossberg at google.com>wrote:

> On 5 February 2013 14:32, Aaron Frost <aaronfrost at gmail.com> wrote:
> > I am trying to understand what should happen if you do a nested
> > destructuring of undefined, where the pattern has a default value
> included.
> > Here is an example of my question:
> >
> > var foo = { bar : { baz : true } };
> > function readFoo({ bar: { baz="DEFAULT BAZ"} }){
> >     console.log(baz);
> > }
> > readFoo(foo); //true
> > readFoo(undefined); //what should happen here
> >
> > Should the second call error because it can't find property baz of
> undefined
> > bar (undefined.baz)? Or should is assign the default because it couldn't
> > locate the value with no error? Possible third option?
>
> That would throw a type error, in both the semantics currently in the
> draft spec as well as the modified "refutable" semantics that was
> discussed at the last meeting
> (http://wiki.ecmascript.org/doku.php?id=harmony:refutable_matching).
> With what is proposed on the wiki, however, you can actually make this
> succeed by explicitly marking the whole pattern as "soft" with a '?':
>
>   function readFoo(?{ bar: { baz = "DEFAULT BAZ"} }) {
>       console.log(baz);
>   }
>
>   readFoo(undefined);  // DEFAULT BAZ
>
> /Andreas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130205/2230119a/attachment-0001.html>


More information about the es-discuss mailing list