An array destructing specification choice

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Nov 5 17:15:58 PDT 2011


On Nov 5, 2011, at 12:38 PM, Erik Arvidsson wrote:

> On Sat, Nov 5, 2011 at 11:55, Brendan Eich <brendan at mozilla.com> wrote:
>> The issue with ... in an array destructuring pattern is different from the case without. We have a choice, as you say. It's not obvious that doing a "get" of 'length' on the RHS (once per ...) is the right answer. It's plausible in my view that ... captures all indexed properties (index as defined by ECMA-262).
> 
> // A
> var [x, y, z] = {0: 0, 1: 1, 2: 2, length: 0};
> `$x $y $z`  // '0 1 2'
> 
> // B
> var [x, ...xs] = {0: 0, 1: 1, 2: 2, length: 2};
> `$x [$xs]`  // '0 [1]'
> 
> I think A is clear. It is just syntactic sugar for x = $tmp[0] etc. No
> need to check length. In this case the lhs drives this decision.
> 
> B IMO must check the length or it would have to iterate over all
> own(?) properties in rhs instead of just iterating over startIndex to
> length - 1. It is also more consistent with other operations that work
> on array like objects (slice, apply, splice...).
> 
> In Traceur we do the following:
> 
> var x, xs;
> (function($0) {
>  x = $0[0];
>  xs = Array.prototype.slice.call($0, 1);
>  return $0;
> }).call(this, {
>  0: 0,
>  1: 1,
>  2: 2,
>  length: 2
> });
> 
> I believe this is simpler to understand than to say that all indexed
> properties are used.

Just to be explicit about this point, the above use of slice is dependent upon length in exactly the way I'm suggesting...

Allen





More information about the es-discuss mailing list