I'm trying to understand the reasoning behind why the language designers would make the for (.. in ..)
loops so verbose. For example:
for (var x in Drupal.settings.module.stuff) {
alert("Index: " + x + "\nValue: " + Drupal.settings.module.stuff[x]);
}
It makes trying to loop over anything semi-complex like the above a real pain as you either have to alias the value locally inside the loop yourself, or deal with long access calls. This is especially painful if you have two to three nested loops.
I'm assuming there is a reason why they would do things this way, but I'm struggling with the reasoning.
1 Answer 1
So your complaint is that the for (var key in set )
loop in Javascript loops the index, and not the values (or both) like foreach ($array as $key => $value)
like PHP (and a number of others) do?
I think this is an attempt to be more inline with a standard for-loop, compare:
for (var index = 0; index < set.length; index++) {
f(set[index]);
}
for (var key in set) {
f(set[key]);
}
Consistency is achieved this way between the original form, splitting the key/value pair and returning both may well be very useful and indeed I find the JavaScript method a pain, but it is very clean.
What language did it borrow from originally? What is the method there - the Perl/PHP style of foreach()
is a different root.
Personally, if I'm using JavaScript, I am using jQuery, and that has jQuery.each():
$.each(set, function(key, value) {
f(value); // or f(this); -- though 'this' will be an object in all cases
});
-
1I'm not aware of any language that loops over the keys by default. Most languages provide some way of extracting the keys so you can loop over them seperately if that's really what you care to do. To me, it seems strange to place such a high priority on the keys though when you are presumably looping over an array because you want to access the contents.Matthew Scharley– Matthew Scharley2011年02月16日 01:00:10 +00:00Commented Feb 16, 2011 at 1:00
-
@Matthew Scharley: If you are viewing it as a
foreach()
an iterator, then no, it is uncommon I suppose. But if you view it as a variant on the standardfor()
loop, which it is, then it makes sense. Not saying it is preferable.Orbling– Orbling2011年02月16日 01:07:46 +00:00Commented Feb 16, 2011 at 1:07 -
@Orbling: I see what you are saying, but it is structured as a
foreach
style construct. Now that you put it this way it makes sense, I still say it is counter intuitive though. As for jQuery, I'm in the same position. I'm still hesitant to make a call to$.each
every time I want to do a loop though.Matthew Scharley– Matthew Scharley2011年02月16日 01:25:40 +00:00Commented Feb 16, 2011 at 1:25 -
1I can promise you that a call to $.each is not going to break the performance of your web application. What you do in that iterator might, but not the each itself.Jordan– Jordan2011年02月16日 07:29:28 +00:00Commented Feb 16, 2011 at 7:29
-
2@Mathhew: By default, iterating over a dict (associative array, i.e. similar to JS objects) Python only gives the keys. The reason is that otherwise
for x in xs: assert x in xs
breaks (which would be quite weird) and(key, value) in d
is rarely useful. You can easily usefor val in d.itervalues(): ...
andfor key, val in d.iteritems()
though.user7043– user70432011年02月16日 14:55:42 +00:00Commented Feb 16, 2011 at 14:55
for(var i = 0; i < x.length(); i++)
.length
is a property, not a method.