Subj : Re: "for each" bug To : netscape.public.mozilla.jseng From : Martin Honnen Date : Tue Apr 26 2005 06:13 pm Jason wrote: > The bug is in the "for each" keyword... which is odd because I can't > find any documentation of there actually being a "for each" keyword in > Javascript (but *you* try doing a web search on "for each" and see > where it gets you!!!), though there seems to be something in the > Spidermonkey source tree (JSOP_FOREACH in > http://lxr.mozilla.org/mozilla/source/js/src/jsparse.c). There is no "for each" statement in ECMAScript edition 3, the standard for JavaScript, but an extension to that standard called E4X (ECMAScript for XML) introduces an "for each" statement and Spidermonkey by now implements that extension. The specification for E4X is here: > Here's the bug: > > Side effects seem to happen in > a variable unrelated to the for each loop (note that the > outer for() loop morphs into a "for each()" loop on its > second and subsequent iterations) > > function foreachbug(do_bug) > { > var i,j,k; > var d_a; > var Tlookupi = []; > Tlookupi[7] = "element seven"; > Tlookupi[6] = "element six"; > Tlookupi[8] = "element eight"; > d_a = ["bing","bong","boom"]; > > for (j in Tlookupi) > { > print("\n"+j+"\n"); > for (i = 1; i <= 3; i++) > { > if (do_bug) > { > for each (k in d_a) > print(k+":::"); > } > else > { > for (k in d_a) > print(k+":::"); > } > } > print("\n"+j+"\n"); > } > } > > js>foreachbug(false) > > 7 > 0:::1:::2:::0:::1:::2:::0:::1:::2::: > 7 > > 6 > 0:::1:::2:::0:::1:::2:::0:::1:::2::: > 6 > > 8 > 0:::1:::2:::0:::1:::2:::0:::1:::2::: > 8 > js>foreachbug(true) > > 7 > bing:::bong:::boom:::bing:::bong:::boom:::bing:::bong:::boom::: > 7 > > element six > bing:::bong:::boom:::bing:::bong:::boom:::bing:::bong:::boom::: > element six > > element eight > bing:::bong:::boom:::bing:::bong:::boom:::bing:::bong:::boom::: > element eight Looks indeed like a bug to me, you could file one on bugzilla. -- Martin Honnen http://JavaScript.FAQTs.com/ .