An array destructing specification choice

Brendan Eich brendan at mozilla.com
Sat Nov 5 11:55:09 PDT 2011


On Nov 5, 2011, at 9:38 AM, Allen Wirfs-Brock wrote:

> In a similar vain, what is the value of r in:
> 
> let [z,y,...r] = {0:0, 1:1, 2:2, length: 3, 3:3,4:4};
> 
> should it be [2] or [2,3,4]  (and if the latter how is that determined)?

The inspiration for ... in the past came from (among other sources) Successor ML:

http://successor-ml.org/index.php?title=Functional_record_extension_and_row_capture


> It seems to me that [2] is the right answer

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

/be


> and if that is the cause, consistency requires that for the first problem z gets the value undefined.
> 
> Allen
> 
> On Nov 5, 2011, at 9:28 AM, Allen Wirfs-Brock 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?
>> 
>> Allen
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>> 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list