On Jul 5, 2012, at 10:54 AM, Brendan Eich 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.

Specifically, for consistency, I think

      array.forEach((v)=>console.log(v));

and

     for (let v of array) console.log(v);

should yield the same results.

[].forEach skips "holes" so the default iterator for arrays should too.  All 
the other "Array extras" also have the skipping behavior. 

> 
> Map may win at some point, who knows? It's not winning if one wants an array, 
> numeric indexing, .length, the usual prototype methods.

We could consider also have "dense" interators available:

     for (let v of array.denseValues) console.log(v);


Allen




> 
> /be
> 
> Jason Orendorff wrote:
>> On Thu, Jul 5, 2012 at 6:11 AM, teramako<[email protected]>  wrote:
>>> Firefox 15 or later can use for-of statement.
>>> I have a question about this.
>>> 
>>>     var array = new Array(3);
>>>     for (var i in array) {
>>>       console.log(i);
>>>     }
>>>     for (var v of array){
>>>       console.log(v);
>>>     }
>>> 
>>> This for-in code logs nothing of course but for-of code logs 3 of undefined.
>> [...]
>>> Are these behaviors correct ?
>> 
>> Well, they're certainly intentional. There's no final specification
>> yet, so it could change.
>> 
>> The rationale for this is that for-of should act like the canonical
>> for-loop over an array:
>> 
>>     var array = new Array(3);
>>     for (var i = 0; i<  array.length; i++)
>>         console.log(array[i]);  // "undefined" 3 times
>> 
>>     for (var v of array)
>>         console.log(v);  // "undefined" 3 times
>> 
>> The corresponding for-in loop is much rarer, with good reason. Note
>> that "for-in" on an Array is underspecified (in terms of the order in
>> which the property names are visited and what happens when properties
>> are added or removed during iteration). The behavior we implemented
>> for Array iterators is straightforward and easy to use, and it's
>> easier to specify fully. See
>> <http://wiki.ecmascript.org/doku.php?id=harmony:iterators>.
>> 
>> I think Map will end up being better than Array for sparse collections.
>> 
>> -j
>> _______________________________________________
>> es-discuss mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/es-discuss
>> 
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
> 

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to