What kind of language is that ?
Consider the following code :
for (var i = 0; i < 20; i++) {
var j = i;
}
alert(i + ' ' + j);
That's javascript. It gives "20 19". Would you expect that from a decent language ?
Update: Same result with a var j = 0;
before the for
loop. No surprise, actually.
Update 2: It seems what I'm complaining about has not been well received ;). I'm not complaining about the values, I'm complaining about the fact that there are values...
2006-03-11 19:20:42+0900
Both comments and pings are currently closed.
2006-03-11 19:49:03+0900
C++ does the same thing.
2006-03-11 19:56:42+0900
This is exactly what I would expect, barring variable scope problems.
2006-03-11 20:09:00+0900
Yes, I would expect that from any good language. (I don’t say that JS is good)
Maybe you should unroll the loop in your head.. :)
2006-03-11 20:12:47+0900
Eh, you need to surpass the condition actually. So the result makes perfectly sense to me. (At least that’s what I expect from for loops. I like constructs like each more, though.)
2006-03-11 20:45:12+0900
Existence of these variables outside the loop… berk!
2006-03-11 21:27:20+0900
JavaScript does not start a new scope for { } blocks. Only for function (object) declarations.
http://jibbering.com/faq/faq_notes/closures.html
Try this:
for (var i = 0; i < 20; i++) {
(function () { var j = i; })();
}
alert(i + ‘ ‘ + j);
2006-03-11 23:19:28+0900
I’m soundly in the school of coder, that needs a language that doesn’t do this sort of thing.
Hey, if they hadn’t called it JAVAscript would you even try and compare it to a real language? But then shells have funny scoping for file handles, it is the idiosyncracies that stop normal (and/or intelligent people) from coding.