The reason why he doesn't like putting the var in the for loops has to
do with variable hoisting and non-block scope in javascript.
for(var i = 0; i < 5; i++) {
print(i);
}
i; // undefined in java but 5 in javascript
He would rather people define the variable outside of the for loop so
that people realize that the variable is still available after the for
loop ends.
var i = 0;
for(i = 0; i < 5; i++) {
print(i);
}
i; // 5
A page on MDC (I guess MDN now) about Scope and hoisting:
https://developer.mozilla.org/en/JavaScript/Reference/Scope_Cheatsheet
------------------------
Gary Katsevman
Computer Science Undergraduate
Northeastern University
gkatsev.com
On Fri, Jan 14, 2011 at 15:41, cancel bubble <[email protected]> wrote:
> I'm with you, I personally prefer vars in the for loop. I'm guessing it's
> not a bug but enforcing the best practice style I seem to be seeing more and
> more. I don't have my Good Parts book handy at the moment, but I'm guessing
> this style with for loops is in the book. What sucks the most is it stops
> reporting any other errors and I'm much more interested in more critical
> errors I can catch. For now, I'm just going to use the older jshint and
> I'll check jslint every now and again to see if the behavior has changed.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]