An array destructing specification choice

Brendan Eich brendan at mozilla.com
Sat Nov 5 14:41:20 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.

Yay, agreed.


> 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.

That's what row capture must do when ... is used in an object pattern. Are you worried about performance?


> It is also more consistent with other operations that work
> on array like objects (slice, apply, splice...).

True.


> 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.

Good, but more important than the exception building upon an existing understanding is your willingness to avoid getting 'length' if the array pattern has no ... in it. That is a kind of consistency argument that Allen made, which I don't prioritize very high among countervailing arguments.

We have:

1. Should an array pattern always query 'length'?

2. If the answer to (1) is "no", then should ... in an array pattern query 'length'?

On reflection and at this point in the thread, with your reply in mind, my prefs in order: [no, yes], [no, no]. In no case do I favor [yes]. I'm refutably matching [no, _] :-P.

/be


More information about the es-discuss mailing list