An array destructing specification choice

Brendan Eich brendan at mozilla.com
Sat Nov 12 12:02:01 PST 2011


On Nov 12, 2011, at 2:07 AM, Axel Rauschmayer wrote:

>> Embedded spreads is already in the ES6 draft for  both array literals and argument lists.  I'm not at all sure that embedded rests in destructurings are such a good idea.
> 
> I think it would be nice to express “last element(s)", e.g. in function parameters (where the callback is usually at the end):
> 
>      foo(arg1, arg2, ..., argn, callback)

Yes.


>> Just to start with, what does this mean:
>> 
>>   [a,b,...r,c,d = [1,2,3]
> 
> That seems related to
>      [a, b, c, d] = [1,2,3]
> and to
>      [a,b,...r,c,d] = [1,2,3,4]
> 
> Hence, I would guess:
> a === 1
> b === 2
> r === []
> c === 3
> d === undefined

Why guess? There is no requirement that ...r consume 1 element, but there is a requirement in Dave's desugaring that trailing non-rest element patterns consume elements up to the one indexed by rhs.length - 1.


> I agree with Claus Reinke that naming is tricky: Is destructuring assignment the reverse of invoking a constructor?

No, and there's no antonym there: construct <=> destruct, not destructure.


> Is it a desctructor, then? But that clashes with C++ terminology.

Of course.

Structuring <=> Destructuring. When you write object literals, which desugared in ES3 to assignments following a construction via new Object or new Array (with all the vulnerabilities that looking up Object and Array in the current scope, and running prototype setters, entail). In ES5 the initialisers use [[DefineOwnProperty]], important fix. There's no good jargon for the ensemble effect, though: "object / array literal" or "object / array initialiser" (UK spelling) is too much. "Structuring" is what "destructuring" suggests and it works for me.

/be



More information about the es-discuss mailing list