On Nov 5, 2011, at 9:44 AM, Brendan Eich wrote:
> 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?
>
> What I implemented long ago in SpiderMonkey based on the ES4 proposal gives
> z=2. I still think that's the best answer.
>
> Destructuring is "irrefutable" in that it desugars to assignments from
> properties of the RHS. It is not typed; it is not refutable (no one mention
> Erlang -- oops, Dmitry did; ok, refutable match is a separate beast, proposed
> but deferred: wiki.ecmascript.org/doku.php?id=strawman:pattern_matching). It
> should not impose any particular constraints on the RHS object based on the
> LHS pattern.
>
> In this case, the RHS object is not consistent with Array invariants anyway.
> That means as much, if not more, than the array pattern. Someone may have set
> it up that way for a reason. It could have come from a JSON deserialization.
> The array pattern should not check for 'length' and enforce Array invariants
> that do not apply inherently on the RHS.
But all other array operators and functions use "length" to limit their bounds
when dealing with array-like objects. Also, via inheritance a real array can
acquire an integer keyed property that is beyond its length bound:
let [z,y,z] = {2:2} <| [0,1];
Allen
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss