undefinedfix
Sign in

I don't understand this variable

vonhx edited in Mon, 05 Apr 2021
for (let i = 0; i < 5; i++) {
            a[i] = function() {
                console.log(i)
            }
        }

        a[2]() //2

Why is I not undefined? I just create a function in the for loop. Why can I still get the value of I when I execute a [2]

7 Replies
yfrms
commented on Mon, 05 Apr 2021

Therefore, let is only valid at the current i-level. In fact, your loop creates five block level scopes, each of which has an effect only in the current block level scope.

Your code is equivalent to the following:

"use strict";

var _loop = function _loop(i) {
    a[i] = function () {
        console.log(i);
    };
};

for (var i = 0; i < 5; i++) {
    _loop(i);
}

a[2]();

It's easy to see here: in fact, the existence of let keeps every I value in a closure. So this loop creates five closures. The a array contains a bunch of functions, each of which accesses the I value in the corresponding closure.

So the value of I must exist, not undefined.

When you don't understand ES6 syntax related code, you can use Babel's online tools to convert ES6 examples into Es5 code. Online address
user618424
commented on Tue, 06 Apr 2021

Didn't you create five functions?

a[0] = function(){
console.log(0);
}
a[1] = function(){
console.log(1);
}
a[2] = function(){
console.log(2);
}
.....
toting
commented on Tue, 06 Apr 2021

Let keyword will form the for loop into a closure, and the console.log (i) Here I is 0,1,2,3,4. Then you will print out the corresponding value

var i,
    a = [];
for(i=0; i<5; i++){
  (() => {
    a[i] = () => { console.log(i); }   //这里面的所有i都是外面传入进来的,循环一次创建一次函数。
  })(i);
}
Amer
commented on Tue, 06 Apr 2021

clipboard.png

Did Mr. Ruan explain it very clearly

bakapasanac
commented on Tue, 06 Apr 2021

This is a let usage problem. First of all, it should be pointed out that it will not be undefined no matter what

  • If VaR is used in the for loop to declare I, then I is a global variable. After the for loop, the value of I is 5. If any function in a is executed, the result is 5
  • Let declaration is used in the problem. Let has block level scope, and each I is a local variable of its corresponding loop. The principle that you can get the value by executing a [2] is closure, which can get the local variables in the block level scope represented by each loop
011915
commented on Tue, 06 Apr 2021

Each cycle in ES6 is an independent block level scope. The variables in the block level scope are referenced by global variables. The current block level scope is not destroyed. Therefore, when a [2] function is executed in the global scope, I can be found in the superior scope defined by a [2] function

RealSpeedyGulfport
commented on Wed, 07 Apr 2021

Let is something in ES6; in ES6, the for loop forms an independent block level scope; let has a block level scope, and each I is a local variable of its corresponding loop. If you execute a [2] to get the value of the loop, you can get the local variables in the block level scope represented by each loop

lock This question has been locked and the reply function has been disabled.