Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not encouraged to create functions within a loop in JavaScript? [duplicate]

I am an absolute newbie, and I just read this in JavaScript: The Good Parts.

In the chapter talking about scope, it says "It is important to understand that the inner function has access to the actual variables of the outer functions and not copies in order to avoid the following problem." And then the two following examples look like this:

//BAD EXAMPLE

var add_the_handlers = function (nodes) {
    var i;
    for (i = 0; i < nodes.length; i += 1) {
        nodes[i].onclick = function (e) {
            alert(i);
        };
    }
 };

//END BAD EXAMPLE

var add_the_handlers = function (nodes) {
    var helper = function (i) {
        return function (e) {
            alert(i);
        };
    };
    var i;
    for (i = 0; i < nodes.length; i += 1) {
        modes[i].onclick = helper(i);
    }
};

According to the author the second example is better because it doesn't use a loop inside the function, otherwise it could be wasteful computationally. But I am at loss and don't know what to do with them. How do I put his theory in real application? Can anyone illustrate these two examples combine HTML?

like image 339
HanChen Avatar asked Jan 18 '17 03:01

HanChen


2 Answers

The problem is with closure. The inner functions have access to the variable i defined outside of these functions. After all iterations of the loop have been executed, the variable i will hold the value of nodes.length. So when you click on nodes[0], the alert will say nodes.length, which is not what you'd expect. (You would expect the alert to say 0.) The same holds when you click on nodes[1], nodes[2], etc. The alert for all of them will say nodes.length.

like image 63
Kevin Le - Khnle Avatar answered Sep 19 '22 18:09

Kevin Le - Khnle


Firstly, in the bad example, a function is created for each event handler; the loop creates multiple function objects. Whereas in the second example, a single function is created and referenced from inside the loop. So you save a lot of memory.

Secondly, in the bad example, as the value of "i" runs, the function does not retain the value, and when it runs, it will always return the last value of "i". In the good example however, as "i" is passed into the function, this value is retained as the lexical environment of the function, and when it is called, it will return the correct value.

Thirdly, as mentioned by @Gary Hayes, we might want to use the function elsewhere too. So it's best to keep it independent of the loop.

like image 30
poushy Avatar answered Sep 19 '22 18:09

poushy