0

Following code is given:

var a = [ ], i = 0, j = 0;

for (i = 0; i < 5; i += 1) { 

  (function(c) {
    a.push(function () { 

    console.log(c); });

  })(i);
};

for (j = 0; j < 5; j += 1) { a[j](); } 

Why does i always get bigger by 1 instead of staying 5? Hasn't the foor loop already been passed, so the i parameter given to the anonymous function should be 5?

1
  • 1
    Because you are creating a new scope in each iteration, by calling a function, which captures the current value of i. Commented Aug 17, 2014 at 19:40

2 Answers 2

3

If you referenced i from the inner closure then yes, you would see the result being 5 in all cases. However, you pass i by value to the outer function, which is accepted as parameter c. The value of c is then fixed to whatever i was at the moment you created the inner closure.

Consider changing the log statement:

console.log("c:" + c + " i:" + i);

You should see c going from 0 to 4 (inclusive) and i being 5 in all cases.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer, so i basically create for every loop an inner closure, which has access to its own i value?
@AndyB Kind of, technically you create inner closures that each have their own c variable, each of which is initialized to the value that i held when the closure was created.
2

chhowie's answer is absolutely right (and I upvoted it), but I wanted to show you one more thing to help understand it. Your inner function works similarly to a more explicit function call like this:

var a = [ ], i = 0, j = 0;

function pushFunc(array, c) {
    array.push(function () { 
        console.log(c); 
    });
}

for (i = 0; i < 5; i += 1) { 
    pushFunc(array, i);
}

for (j = 0; j < 5; j += 1) { a[j](); } 

Which should also help you understand how c comes from the function argument, not from the for loop. Your inner function is doing exactly the same thing as this, just without an externally declared named function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.