1. Create functions

1. Create a heap; 2. Store the code in the function as a string; 3

Where is the function created and what is the parent scope of its execution

2. Function execution

1. Form a new private scope (stack memory, execution context), execute once, form once, after execution by JS garbage collection mechanism, will not affect each other 2. Parameter assignment, variable promotion. 4. When executing a variable, first check whether it is a private variable. If it is a private variable, then operate on the private variable. The relationship between private variables and external variables is understood as the private stack memory protection mechanism, which is the closure protection mechanism

3. When a function is executed, it will form a closure, but it will be freed after execution.

function fn() {let i=1
    return function (a){
        console.log(++i,++a)
    }
}
letF =fn() f(1) /2,2 f(1) /3,2 f(3) /4,4Copy the code