destructuring: as patterns?

Brendan Eich brendan at mozilla.org
Fri Apr 20 14:43:50 PDT 2012


Allen Wirfs-Brock wrote:
>> >  
>> >  js>  let {y:{z}} = {};
>> >  typein:4: TypeError: (void 0) is undefined
>
> alternatively:
>     let {y:{z} = {z:"default"}} = {};
> or
>     let {y:{z = "default"}} = {};

Too verbose, for one.

>> >  (Atrocious SpiderMonkey failure to pretty-print the blamed expression instead of its portable (void 0) value there -- my fault I think.)
>> >  
>> >  Dave suggested making the first case, let {x} = {}, throw, and requiring ? as a pattern modifier (I suggested prefix):
>
> Then shouldn't
>     let x;
> be illegal?

No. I know destructuring requires an initialiser but let does not and I 
think should not. It's a different beast. What you seem to be suggesting 
is that we relax the initialiser requirement, not that we ban let x.

>    Would you have to say:
>     let ?x;

No way!

>> >  let {?x} = {}; // x is undefined, no throw
>> >  let {y} = {};  // throws
>
> so, why not:
>
>    let {x=undefined} = {};
>
Again, too verbose and (this is the part I left out, see followup) not 
deep. How would defaulting work here?

   let {x:{y:{z}}} = {};

/be


More information about the es-discuss mailing list