An array destructing specification choice

Brendan Eich brendan at mozilla.com
Sat Nov 5 11:32:12 PDT 2011


On Nov 5, 2011, at 9:59 AM, John J Barton wrote:

> On Sat, Nov 5, 2011 at 9:28 AM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
>> In the following declaration, what should be the value of z?
>> 
>> let [z,y,z] = {0:0, 1:1, length: 2, 2:2};
>> 
>> should it be be 2 or undefined
>> 
>> undefined might be reasonable because it is an array pattern, and the source object is only has two "array-like" elements
>> 2 might be reasonable because the source object actually has a property named "2"
>> 
>> Which alternative will be least surprising to JS programmers?
> 
> (Oh great more features that turn programming into little brain teasers :-( )
> 
> Devs need an algorithm:

Agreed!


> let _lhs = [];
> let _rhs =  {0:0, 1:1, length: 2, 2:2};
> _lhs[0] = _rhs[0];
> _lhs[1] = _rhs[1];
> _lhs[2] = _rhs[2];
> let z = _lhs[0];
> let y = _lhs[1];
> let z = _lhs[2];
> 
> So z would be 2.
> 
> I guess the algorithm can be shorter:
> let _rhs =  {0:0, 1:1, length: 2, 2:2};
> let z = _rhs[0];
> let y = _rhs[1];
> let z = _rhs[2];

This is exactly the simple, local transformation we specified for ES4 destructuring (wiki.ecmascript.org/doku.php?id=proposals:destructuring_assignment#notes), first implemented for array patterns by Opera and I hope it carries over. Anything else is more complex, requiring a 'length' check before rhs[i] get.

/be



More information about the es-discuss mailing list