An array destructing specification choice

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Nov 5 10:19:49 PDT 2011


On Nov 5, 2011, at 10:04 AM, Dmitry Soshnikov wrote:

> On 05.11.2011 20:54, Allen Wirfs-Brock wrote:
>> On Nov 5, 2011, at 9:45 AM, Dmitry Soshnikov wrote:
>> 
>>> On 05.11.2011 20:38, 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)?
>>>> 
>>>> It seems to me that [2] is the right answer,
>>> How so? If `...n` is the range slice operator, it should be [2, 3, 4], no?
>> How do you define the range of "array-like elements" of an object.  In all other places of the ES specification, the value of the "length" property is used to determine the upper bound of the elements and any "integer keyed" elements beyond the length are ignored by array operations and functions.
>> 
> 
> Let's see.
> 
> What is the result in case of:
> 
> let a = [0, 1, 2, 3, 4];
> a.length = 6;
> 
> let [z, y, ...r] = a;
> 
> if `...r` depends on the `length` of `a`, then `r` is [2, 3, 4, undefined]; If not, then `r` just takes only existing values (as some array methods do, e.g. forEach or map), and it's [2, 3, 4]. In later case, the simple object semantics obeys this rule.

As the draft ES6 spec. is currently written, r will be [2,3,4,,]    (r.length= 4).  In other orders, "holes" holes are captured by the result argument.  This is all pretty consistent with "array" processing throughout the Es spec. 


Allen


More information about the es-discuss mailing list