Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
4  
I prefer ye old for(var i = 0; i < x.length(); i++). –  muntoo Feb 16 '11 at 1:21
1  
Gawd ... I get it, but it is awkward indeed. I would rather learn Chinese than a weird computer language. –  Job Feb 16 '11 at 4:00
    
@muntoo: length is a property, not a method. –  ThiefMaster Oct 6 '12 at 10:46
    
@ThiefMaster: Ah well, I don't really program in JavaScript. Thanks for the correction. –  muntoo Oct 6 '12 at 19:38

1 Answer 1

up vote 5 down vote accepted

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
});
share|improve this answer
1  
I'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 Feb 16 '11 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 standard for() loop, which it is, then it makes sense. Not saying it is preferable. –  Orbling Feb 16 '11 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 Feb 16 '11 at 1:25
1  
I 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 Feb 16 '11 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 use for val in d.itervalues(): ... and for key, val in d.iteritems() though. –  delnan Feb 16 '11 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.