In order to fully understand this cliché, check to find out, it is preliminary to know what the ghost, how to use, why use closure: an intrinsic function defined by an external function is a closure. The role and benefits of closures: closures create conditions for accessing internal variables defined by external functions. It also encloses everything about the function inside the function, reducing the global variable, which is the true meaning of the closure. prior to understanding closures. It's better to understand the meaning of the scope chain, simply, a scope chain is a function that is created at the time of definition and used to find an index of the value of the variable used, and his internal rules are, Put the local variables of the function itself first, put the variables in its parent function next, put the variables in the higher-level function later, and so on until the global object . when the function needs to query the value of a variable, the JS interpreter will go to the scope chain to find, From the first local variable, if the corresponding variable is not found, then to the next level of the chain to find, once the variable is found, it will no longer continue. If the required variable is not found at the end, the interpreter returns undefined. to understand the scope chain, Let's take a look at the memory recycling mechanism of JS, generally speaking, a function at the beginning of execution, the variables defined in the memory space to save, in case the subsequent statements are used, wait until the function has been executed to return, These variables are considered useless. The corresponding memory space is also recycled. The next time you execute this function, all of the variables go back to their original state and re-assign the values to use . but if another function is nested inside the function, And this function is likely to be called externally. And this internal function uses some variables of the external function, . this memory-recycling mechanism will be problematic . if the external function is returned and the intrinsic function is called directly, Then the intrinsic function cannot read the value of the variable in the external function that he needs. . so the JS interpreter will automatically save the function and the variable (free variable) that he might use, including the local variable and the parent and ancestor-level functions, when it encounters the function definition. That is, to build a closure, These variables will not be reclaimed by the memory collector, only if the internal function is not possible to be called (for example, if it is deleted or without a pointer), the closure will be destroyed, and none of the variables referenced by the closure will be recycled when the next memory recycle is started . Differences from normal functions: 1, the normal function can also expose the internal values. Method A defines a global variable, but the memory that is occupied cannot be freed and the variables used by the function are defined outside the function and are not easy to understand and manage. PartyLaw b passes an internal variable as a parameter, and this method is too ugly. 2, each time the function executes and initializes only its internal variables, the maximum difference between closure and normal function is caused. Is that each time a normal function is called, it is initialized to a consistent state, resulting in a consistent result of execution. Closure, its essence is an intrinsic function, the call closure will only initialize the internal function variables, the external function of the variable is not initialized, the implementation of the value of the variable is passed. An external function is initialized only when a closure is defined. Memory is recycled when closures die. when to use closures:
The global variable B is changed every time the function A is called, and B is only related to a. In the past, you can define B as a global variable only if you have no closure, and B is now defined as an internal variable of a, and closure C is defined inside a, and C is returned. To write something, write it:
1 functionGG () {2 varb = 0;3 return function(){4b++;5 returnb6 }7 }8 vartt =GG ();
9TT ()//1TenTT ()//2 OneTT ()//
Closures are characterized by a distinct, closed package in which variables cannot be released, cannot be accessed directly, and closures can be deferred. So you can use it to do something: manage private variables and private methods, and encapsulate changes to variables (states) in a secure environment, because some of the resources within the closure are not automatically released, causing memory leaks
JS Closed Packet Understanding