For Each Loop in Mozilla
I’m not entirely sure when Mozilla started to support for each loops but I guess it was part of the E4X code checkins. Anyway, I was totally shocked when I saw it and I fired up a quick test.
var o = {a: "A", b: "B", c: "C"};
for each (var v in o) {
alert(v);
}
var a = ["A", "B", "C"];
for each (v in a) {
alert(v);
}
window.onload = function () {
var sb = [];
for each (var el in document.getElementsByTagName("*")) {
sb.push(el.tagName);
}
alert(sb.join("\n"));
};
Thank god for Mozilla
September 2nd, 2005 at 2:55
Wrong in FireFox or Opera.
by the way, what’s E4X?
September 2nd, 2005 at 3:24
No, it works prefectly if you are using Deer Park Alpha 2 (not sure about Alpha 1, probably works too). E4X stands for ECMAScript for XML.
“for each” seems to be more suitable instead of “for in”, which scans all the object under the subject, e.g. length in Array.
Try to rewrite the second for each loop to for in loop and you’ll notice the differences.
September 2nd, 2005 at 3:30
Correction: rewriting the second loop will give the same result. I can’t find the right example yet.
September 2nd, 2005 at 10:20
Thats great news. While not the most exciting addition they are quite convenient.
September 8th, 2005 at 20:29
I don’t get the point…
Isn’t this exactly what you have been able to do for years using
for (i in v){}
Or is there some subtle difference? (Now I see, it returns the actual object, not the index)
var o = {a: “A”, b: “B”, c: “C”};
for (v in o) {
alert(o[v]);
}
var a = ["A", "B", "C"];
for (v in a) {
alert(a[v]);
}
window.onload = function () {
var sb = [];
for (el in document.getElementsByTagName(”*”)) {
sb.push(document.getElementsByTagName(”*”)[el].tagName);
}
alert(sb.join(”\n”));
};
seems to be working fine cross-browser.
So right, this is convenient, but nowhere near revolutionary…
September 8th, 2005 at 20:42
It is nothing revolutionary. The good thing is that Mozilla realizes that there is a need to improve JS 1.x.
September 9th, 2005 at 13:52
Hi,
Well, in that case…
- Mozilla can render HTML, thank god for Mozilla.
September 9th, 2005 at 16:16
Hakan: I’m not sure what you are trying to say here?
September 9th, 2005 at 16:49
I was a little bit sarcastic. Even thoguh this ain’t so big a deal, you’re taking every opportunity to exaggerate Firefox. ‘Coz the feature with different version of browsers, and code that works on a few of them, is also available on IE. tihi
Anyway…hope you aren’t offended. I’m just expressing differing opinions.
September 9th, 2005 at 17:29
HBI! Great to see that you read Erik’s blog. (I found it through challenger’s home page, so I should’ve guessed)
You’re post is a bit of freudian mis-saying. If you had added an X to HTML that would be a totally true statement.
Fact is that IE is a browser which barely has been updated for a few years. (Except for security patches) Alot can be done with the custom DOM of IE, but you can’t escape from the fact that IE lacks proper support for XHTML, and that jscript has quite a few memomry leaks. (Look here: http://erik.eae.net/archives/2005/08/04/21.38.18/ )
September 10th, 2005 at 1:06
nitro2k01,
Long time no see, pal…How’s everything? I read Erik’s blog when ever I can, even though I sometimes don’t agree I appreciate and recognize the guru. Now to my response…
Sometimes I really get confused by the anti-MS propaganda. No to pathes but yes to upgrades? Hmmm…
Many years ago, I was at a seminar. The lecturer said something I find fitting here:
“There are two kinds of people. The ones in the box, complaining about how the walls are too limiting. The other ones, that are outside the box, pushing or pulling the box and taking advantage of the box’s shape, size and color.”
Instead of complaining, Mozart created wonderful music (even though the lack of synths and equalizers) and Da Vinci painted beautiful paintings (even though the lack of Photoshop…haha). Either accept the tools that are at hand and create or don’t.
Alright, I agree…one should always have the right of complaining but don’t dismiss the fact that one can achieve wonders, independently of the tools that are at hand, right now. Erik, Emil and others has prooven that in the past. But enough with this grumbling phase allready…
/hbi
September 10th, 2005 at 13:46
I’m not sure I complain too much. I try identify issues that should not be tolerated in a browser. If you don’t know about the issues it is hard to work around them. Most of the times the issues I describe are with IE but I agree that I haven’t been too good at pointing fingers at Moz, Opera and KHTML. Opera and KHTML/Safari are a lot worse than IE and that is why most applications work in IE and Mozilla. It is just that Opera and KHTML can be ignored in most cases. IE cannot be ignored.
Another reason why I complain about IE: I want MSFT to fix these issues.
A third reason is so more people move away from IE. When people started to move away from IE to Firefox MSFT assembled the old IE team and started its development again. And I want IE and the web to improve.
December 13th, 2005 at 12:47
Very interesting, only in mozilla not correct
view many sites
example : http://games-all.com/
May 26th, 2006 at 11:25
hello
i want to complete definition and example of for each loop. i m not sure in this example what he trying to show.And difference between for in and for each loop.
thanks