for-of statement of sparse array

Brendan Eich brendan at mozilla.org
Thu Jul 5 18:12:22 PDT 2012


Jason Orendorff wrote:
> On Thu, Jul 5, 2012 at 12:54 PM, Brendan Eich<brendan at mozilla.org>  wrote:
>> Allen privately observed that Array forEach skips holes, matching for-in.
>> That counts for a lot with me -- we have only a blind
>> for(i=0;i<a.length;i++)... loop not skipping holes, but of course it
>> wouldn't. That is weak precedent on which to build for-of.
>
> In what sense is that precedent weak? It seems a stronger precedent
> than forEach on every axis; arguably it's one of the most-trodden
> cowpaths in programming, in any language.

That's too general a claim to be self-certifying. I <3 C but in JS, lots 
of a.forEach(function (e) {...}) usage these days. Kids are into FP.

> for-in is a kind of anti-precedent, for array iteration.

The point is for-in *in addition to* forEach and other Array extras skip 
holes. Not for-in by itself as motivation, really, just consistency of 
all but for-of as implemented.

>> Map may win at some point, who knows? It's not winning if one wants
>> an array, numeric indexing, .length, the usual prototype methods.
>
> I agree Map is somewhat beside the point. TC39 should spec what's best
> for developers net of everything. Even if that's matching for-in. But
> the purpose of for-of is to address the shortcomings of for-in;
> following it just for the sake of consistency would be self-defeating.

I think your focus is pulled by for-in too much. It happens to match the 
Array extras and this makes a happy consistency, but the main precedent 
is forEach, along with the rest of the extras which agree on skipping holes.

If there's a botch here, it is holes in arrays, or rather: arrays not 
being like Python lists, instead being barely-specialized 
objects-with-properties. But that's a done deal. From that, holes follow.

 From holes, skipping or not skipping consistently must follow. So far 
we've been consistent over the many years: for-in, forEach, etc. all skip.

The C-style loop is blind to property existence so is really a different 
animal. It's true people are told not to use for-in on arrays, but that 
does not make them use for(;;). Instead they seem to reach for forEach. 
Which skips holes.

Reasoning about latent bugs due to skipping holes is perilous and wants 
empirical studies, some evidence, a few burning anecdotes. Got any?

/be


More information about the es-discuss mailing list