Js closure problem

for(var i=0;i<10;iPP){
    fnArr[i]=(function(){
        var n=i;
        return function(){
            return n
        }
    })();

}
for(var i=0;i<10;iPP){
        (function(){
            var n=i;
        fnArr[i]=function(){
            return n
        };
        })()
    }

all of the above allow fnArr [3] to output 3 instead of 10, but why do you have to declare nSecreti? If you go to this sentence, you will not be able to output it correctly

.
Mar.03,2021

the outer and inner layers are two scopes, and each variable access is the closest. If there is no sentence var n = I , the top-level scope will be accessed, and by this time I has reached 10


if you do not use var I to receive the value of I and return I directly, then the later function cannot find the value of I when it is running, so it will go back to the upper scope to look for it, while the upper scope loop ends, and the value of I is 10, so it will always return 10.

and use var nimi . I want to save the value of I in each loop in the interior of the self-tuning function, and if the value of n cannot be found when the return function is running, it will also go to the upper scope to find the value of n inside the self-tuning function, which corresponds to 0-9 respectively.

can be changed in this way, and the effect is the same

for(var i=0;i<10;iPP){ 
  fnArr[i]=(function(i){ 
    return function(){ 
      return i
    } 
  })(i); 
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9e02b-47777.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9e02b-47777.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?