for-of statement of sparse array

Rick Waldron waldron.rick at gmail.com
Thu Jul 5 22:22:44 PDT 2012


On Thu, Jul 5, 2012 at 6:30 PM, Rick Waldron <waldron.rick at gmail.com> wrote:

>
> On Thursday, July 5, 2012 at 9:18 PM, Brendan Eich wrote:
>
> Brendan Eich wrote:
>
> This upholes the Array forEach (and all other extras) hole-skipping.
> The deck is stacked against for(;;) iteration in my view.
>
>
> LOL, "This upholds", of course.
>
> I had hoped this was a clever pun :)
>
>
> Currently, devs expect for-loop and while (assuming common patterns in
> play here) to be "the expected" way that sparse array holes are exposed --
> so from the "give me what I most likely expect" perspective, I agree with
> the consistency wins argument: for-of should act like for-in
>

To clarify, the behaviours I'm comparing are as follows:

var i, a = [1, 2, , 4];
for ( i in a ) {
  console.log( i, a[i] );
}

0 1
1 2
3 4

var i, a = [1, 2, , 4];
for ( i = 0; i < a.length; i++ ) {
  console.log( i, a[i] );
}

0 1
1 2
2 undefined
3 4


var i = 0, a = [1, 2, , 4];
while ( i < a.length ) {
  console.log( i, a[i] ); i++;
}

0 1
1 2
2 undefined
3 4


Where the latter 2 require an explicit check (not present) against holes.
So I would assume that for-of would behave like...

var i, a = [1, 2, , 4];
for ( i of a ) {
  console.log( i );
}

1
2
4




>
> Rick
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120705/d8889fa8/attachment.html>


More information about the es-discuss mailing list