0

This is the essence of a bigger problem I have:

I get sequence (0, 2), (1, 2), (2, 2) using this code:

var f = [];
var i, a;
for (i = 0; i < 3; i++) {
    a = i;
    f.push(function() { alert(i+", "+a); });
}
for (var i = 0; i < 3; i++) f[i]();

Why are both values different in each alert?

KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
axelbrz
  • 783
  • 1
  • 7
  • 16
  • 2
    possible duplicate of [Javascript closure inside loops - simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – thefourtheye Apr 09 '14 at 12:57
  • never have same name variables for looping. i will have i for upper loop and j for lower loop – Kamran Shahid Apr 09 '14 at 13:06
  • I simplified both codes into one, check the new version of the question. – axelbrz Apr 09 '14 at 13:07
  • Ok, so the scope of i in `for (var i...)` escapes the for scope, even if it's declared inside, thanks. – axelbrz Apr 09 '14 at 13:13
  • Ok, the problem I had was I thought writing `for (var i...)` overrode the previous declaration of `i` as I thought the newest one was going to have a more restricted scope, but that was not what happened, thanks! – axelbrz Apr 09 '14 at 13:22

2 Answers2

1

You're "storing" a function not the value of "a" actually. So when the function is called it's just printing the actual value of a which is the last one set.

What's your goal ?

BTW: shouldn't you have 0,1,2 instead of 1,2,3 in your first example ?

Slade
  • 301
  • 2
  • 7
  • You are right about the sequence, it's 0, 1, 2. I will change the code to show better the problem. – axelbrz Apr 09 '14 at 13:05
  • 1
    Anyway the main issue is that in your function you "alert" a which is a reference to an out-of-function variable. The variable scope is not your function it's shared among all your functions in f[]; As it's shared the value will reflect the last one set which is 2. – Slade Apr 09 '14 at 13:10
  • Ok, so the scope of i in `for (var i...)` escapes the for scope, even if it's declared inside, thanks. – axelbrz Apr 09 '14 at 13:14
  • Ok, the problem I had was I thought writing `for (var i...)` overrode the previous declaration of `i` as I thought the newest one was going to have a more restricted scope, but that was not what happened, thanks! – axelbrz Apr 09 '14 at 13:21
  • Btw, I marked the other answer as the chosen one because the difference was that I was setting the variable `i` each step of the last for. – axelbrz Apr 09 '14 at 13:25
  • np other answer is more clear since you updated the question it makes more sense now. – Slade Apr 09 '14 at 13:32
1

Both i and a are global variables in your example. So when you actually invoke the functions stored in the array, the functions will use the current values of these variables. So it is expected to see

0, 2

1, 2

2, 2

as the result of alerts. This is because when you are looping to call the functions, you are setting i to be 0, 1 and 2; but a remains to be holding the value of 2.

harun
  • 1,889
  • 18
  • 19
  • Yes, the problem I had was I thought writing `for (var i...)` overrode the previous declaration of `i` as I thought the newest one was going to have a more restricted scope, but that was not what happened, thanks! – axelbrz Apr 09 '14 at 13:21