An array destructing specification choice

John J Barton johnjbarton at johnjbarton.com
Sat Nov 5 09:59:30 PDT 2011


On Sat, Nov 5, 2011 at 9:28 AM, Allen Wirfs-Brock <allen at wirfs-brock.com> 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?

(Oh great more features that turn programming into little brain teasers :-( )

Devs need an algorithm:

let _lhs = [];
let _rhs =  {0:0, 1:1, length: 2, 2:2};
_lhs[0] = _rhs[0];
_lhs[1] = _rhs[1];
_lhs[2] = _rhs[2];
let z = _lhs[0];
let y = _lhs[1];
let z = _lhs[2];

So z would be 2.

I guess the algorithm can be shorter:
let _rhs =  {0:0, 1:1, length: 2, 2:2};
let z = _rhs[0];
let y = _rhs[1];
let z = _rhs[2];

I don't understand what " the source object is only has two
"array-like" elements" can mean.

jjb


More information about the es-discuss mailing list