4번독수리의 둥지
use var, always 본문
Use var
, it reduces the scope of the variable otherwise the variable looks up to the nearest closure searching for a var
statement. If it cannot find a var
then it is global (if you are in a strict mode, using strict
, global variables throw an error). This can lead to problems like the following.
function f (){
for (i=0; i<5; i++);
}
var i = 2;
f ();
alert (i); //i == 5. i should be 2
If you write var i
in the for loop the alert shows 2
.
http://stackoverflow.com/a/5717233 “var” or no “var” in JavaScript's “for-in” loop?
'Javascript' 카테고리의 다른 글
jQuery calibrating event coordinate with actual mouse position (0) | 2015.07.21 |
---|---|
Managing jQuery ajax request as an array (0) | 2015.07.16 |
Creating an iframe with given HTML dynamically (0) | 2015.07.13 |
firefox onclick cause TypeError : is not a function (0) | 2015.07.13 |
Overriding CSS dynamically using javascript (0) | 2015.06.23 |