An array destructing specification choice

Brendan Eich brendan at mozilla.com
Sat Nov 5 14:28:05 PDT 2011


On Nov 5, 2011, at 10:24 AM, Allen Wirfs-Brock wrote:

> On Nov 5, 2011, at 9:59 AM, John J Barton wrote:
> 
>> (Oh great more features that turn programming into little brain teasers :-( )
>> 
>> Devs need an algorithm:
>> 
>> 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];
>> 
>> I don't understand what " the source object is only has two
>> "array-like" elements" can mean.
> 
> 
> If you used any of the array functions to process _rhs, they would ignore element 2.  For example:
> 
>  _rhs.forEach(function(v) {print(v)});
> 
> would print: 0 1

Array.prototype.forEach.call(_rhs, function(v){...})

This has too many moving parts to be a desugaring.

Also a mandatory [[Get]] of 'length' from _rhs before the evaluation of the array pattern is too much.  

I don't see why 'length' needs to come into play unless there's a ... in the pattern, or even then. The alternative is to enumerate keys of _rhs and consider all for which key == ToString(ToUint32(key)).

/be


More information about the es-discuss mailing list