JS- closure exercise

First, a first output, because the pre-operation, first participating I output, and then incremented, the output 0

A second output, because f1 and f2 are different functions, not shared variable i, the output is also 0

The third output, because f1, shared i, i is incremented by 1 so the output is 1

let foo = function(){
    let i = 0;
    return function(){
        console.log(i++);
    }
}
let f1 = foo();
let f2 = foo();
f1();// 0
f2();// 0
f1();// 1

 

First, it can be seen from the following functions and closures, the first and second rows of code delusion and thought process, is output.

let x = 100;
let y = 200;
let funA = function(x){
    x += 1;
    let y = 201;
    let funB = function(){
        console.log(x); // 102
        console.log(y); // 201
    }
    return funB;
}
let f = funA(101);
f();

 

Guess you like

Origin www.cnblogs.com/CccK-html/p/11488204.html