javascript scoping in for loop
Posted
by user121196
on Stack Overflow
See other posts from Stack Overflow
or by user121196
Published on 2010-03-17T03:40:40Z
Indexed on
2010/03/17
3:51 UTC
Read the original article
Hit count: 332
JavaScript
|scope
consider the following javascript code, console.log always prints "focus 1" where 2 is the input length. how should I rewrite the code to prints the looping variable i correctly
for (var i = 0; i < inputs.length; i++) { var curIndex = i; inputs[i].addListener('focus', function(thisfield, evt){ console.log('focus '+curIndex); }); }
© Stack Overflow or respective owner