nested destructuring of undefined and default parameters

Andreas Rossberg rossberg at google.com
Tue Feb 5 06:50:07 PST 2013


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


More information about the es-discuss mailing list