Why not making functions within a loop in JavaScript?
Do you know JSLint? A tool which can help you write quality JavaScript code, it is developed by Douglas Crockford. Who is the father of JavaScript, he surely has sufficient background to judge. :) And JSLint is very strict, and you can call it rigid sometimes, but he always has the point. If you have used the tool before, you may have noticed some errors, one of them may look like this : “Don’t make functions within a loop”. OK, let’s fix it, wait, what?
Yes, it may sound absurd at first, let’s dive into to find out what the father wants to say xD
1. Weird code
If you have follow me here from this article What is a closure in JavaScript and why they are wrong. You may still curious about the following code. And if you don’t know closure, you really should check that article first.
1 | var funcs = []; |
Simple code, create a loop first, for each loop, assign the current value i
to to a newly created function. Then start a new loop, execute the functions one by one, something will be output into the console.
This is the result you may expect.
1 | My value: 0 |
But instead, it will output like this:
1 | My value: 3 |
And before you run this code, Mr Douglas has already warned you, ““Don’t make functions within a loop”“. Yes, he make a point, but why?
2. Dissect the mystery.
It is simple because, in that anonymous function created in the loop, you have referred a variable which belongs to the outside scope, so the next time when you execute this function, the variable i
is 3. You will ask, what? Haven’t we store that value during the loop? Let’s rewrite the above code a little.
1 | var i = 0, |
You know the variable hoist
and scope
in JavaScript, right? Which is JavaScript doesn’t have a block scope, so anytime we declare a variable, it will be hoisted to the top of its container function, if there is no container function, it will be hoisted to the global scope. So, this is the reason why i
is appeared at the first of the block. And this is what happens next:
- the variable
i
andj
gets declared first - When the first loop runs, an anonymous function has been created inside the loop
- Inside the newly created anonymous function, it referred a variable
i
which is not in its scope - After the first loop, the value of variable
i
accumulates to 3 since the loop runs for 3 times. - In the second loop, each function created in the first loop will be invoked.
- When it gets invoked, the interpreter will check the value of
i
, and it found there is noi
inside. - Since this anonymous has become a closure, the interpreter will look at its scope chain.
- Finally, the interpreter founds the variable
i
, in the global scope, still within its lexical scope, which is totally legitimate for this anonymous function to refer. - And the value of
i
is3
. We solved it in step 4. - So, a
3
will be output. - What happens afterwards for the second and third loop is totally the same from step 6~10.
You see? The concept of closure in JavaScript makes it very dangerous to make new functions inside a loop. So, Mr Douglas has given us his advice with JSLint.
3. How to solve it?
Since we know the cause, we can solve it now, by using the same concept.
3.1 I love functions and I will create it at any cost!
You may think that since the i
is a primitive value, we could simple declare a new variable inside that function in loop and assign the i
to it. Something like
1 | for (i = 0; i < 3; i++) { |
It won’t work, still give you three My value: 3
.
The reason is still as previous:
When the anonymous function get executed, when it tries to evaluate my_i, it need to read from
i
, andi
is already3
So, one way to solve this is to create another closure to save that temporary value. So it leads us to the following code:
1 | var i = 0; |
We get what we want.
1 | My value: 0 |
What have we done? Simple, we create the function at first, and then, use it to the loop index inside that loop.
3.2 Does the father happy?
OK, if you use JSHint, mostly good, but if you use JSLint, still there are some complaints, I told you before, rigid
, some of the complaints may seems really weird like unexpected ++
, yes, he means when you create that for loop, shouldn’t use i++
, WHAT?!?!?! Yes, father thinks you should use i+=1
… OK, more of this is out of topic.
4. Finally
Now you know why not making functions within a loop.
Since the concept ‘closure’ in JavaScript will it tricky when dealing with the functions created inside a loop. You should always try to create the function first or execute it immediately after the creation.
OK. You know why and you know how. That’s the end of the story. Hope it helps.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.