1. Closure definition: A closure is a function that can read variables inside other functions. In JS, closures can be understood as “functions of functions”. Function fn1(){const m = 3; function fn2(){ alert(m); //3}} 2. Closures are used to access variables inside functions. Keep the values of these variables in memory at all times. 3. Notes in closures: Problem 1. Because closures cause variables in a function to be stored in memory, which consumes a lot of memory, they should not be abused because they can cause performance problems for web pages and can cause memory leaks in IE. Workaround: Assign the value of the variable to NULL before exiting the function. Problem 2. Closures change the values of variables inside the parent function outside the complex function. If you use a parent function as an object, a closure as a public method, and internal variables as private properties, do not change the value of the parent function’s internal variables. 4. Closures find the final value of the corresponding variable in the parent function at the same address. 5. Some examples of closures:
(2) :
(3) :