forEach() trouble when an element is removed during execution

Lars T Hansen lth at acm.org
Mon Sep 3 05:15:21 PDT 2007


On 6/18/07, Jason Orendorff <jason.orendorff at gmail.com> wrote:
> On 6/18/07, Peter Michaux <petermichaux at gmail.com> wrote:
> > I don't think that fixing this edge case to make a very robust forEach
> > would be a bad idea. I think having a very robust forEach (similar to
> > the DOM2 iteration over handlers) would be much better and make the
> > language more self-consistent.
>
> For self-consistency, I think the most important thing is that
> "forEach()" and "for each" behave the same way.  Do you agree?

They don't behave the same way, though.  "ForEach" considers only
properties named by uint-like names below "length", whether enumerable
or not, whereas the latter touches anything enumerable in the object
and prototype chain.  The former is defined as iterating from low
indices toward high indices, whereas the latter enumerates in
insertion order (though how the object and prototype enumerations are
interleaved is not defined, to my knowledge).  Finally, JS1.6 and ES4
define forEach as computing the end index at the beginning of the
iteration, so forEach definitely does not touch properties added (at
the end of the array) during iteration, while "for each" may or may
not.

--lars

>
> I mean, these two things should work the same:
>
>   this.observers.forEach(function(o) {o();});
>
>   for each (let o in this.observers) o();
>
> My point is, I think you're proposing a change to Array iterators,
> not just a change to forEach().
>
> -j
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>



More information about the Es4-discuss mailing list