A closure simply means that a scope can access a local variable inside another function.

The first case:


function fn(){ 
  let num = 10;
  
  function fun(){note: the fun function scope can access the other fn function local variable (num) which generates the closureconsole.log(num)
  }
fun()
}
fn()
Copy the code

The second case: figure code:

  • You can also open Sources at 12F to view closures

The diagram below:

Case study: Closure today interview questions

  • Requirement: Click Li output index number, use closure form implementation

As follows:

  • Conclusion:
  • The function of closure: extends the scope of the variable
  • Disadvantages: Memory does not leak. Advantages: extended the scope of the variable.
  • A function that executes immediately is called a small closure that allows another scoped function to access its local variables.