Closures are a relatively abstract concept, especially for JS novices. The explanation in the book is rather obscure, and it is the same for me.
Closures have two uses, one is to facilitate the implementation of nested callback functions, and the second is to hide the details of the object.
For the former, Nodejs's programming style has been able to explain the problem, the two latter, for the internal variables inside the function is not visible outside, but can provide access to the function to access and modify the corresponding local variables, so as to achieve the intent of OO encapsulation.
But he is also the ability to improve the JS can not be bypassed, almost every interview must ask questions, because in the answer. The depth of your answer, the understanding of terminology and the description of how the JS internal interpreter works can be seen in the actual level of your JS. Even if you don't answer correctly, You can also have an assessment of your level by the examiner. So let me say my understanding of the closure in JS.
Closures are the characteristics of many languages, in JS, closures are mainly related to JS a few other features: Scope chain, garbage (memory) recycling mechanism, function nesting, and so on.
Prior to understanding closures. It is better to understand the meaning of the scope chain, simply speaking, the scope chain is the function at the time of definition created, to find the value of the variable used in an index, and his internal rule is to put the function of its own local variables in the first, the parent function of its own variables in the second, 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 to find, if not found the corresponding variable, then to the next level of the chain to find, once the variable is found, will no longer continue. If the required variable is not found at the end, the interpreter returns undefined.
Understanding the scope chain, we look at the JS memory recovery mechanism, in general, a function at the beginning of execution, it will be defined in the variable memory space to save, in case of the subsequent statements to use, wait until the function is finished to return, These variables are considered useless. The corresponding memory space is also recycled. The next time the function is executed, all the variables return to their original state and are re-assigned. 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 can cause problems. If an intrinsic function is called directly after the external function returns, the 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 variables he or she may use, including the local variables and the variables (free variables) of 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, but only if the internal function is not possible to be called (for example, if it is deleted, or if there is no pointer), the closure will be destroyed, and no one of the closure references will be recycled when the next memory recycle starts.
In other words, with closures, nested function structures work, which is what we expect. Then, closures have some features that are often difficult for programmers to understand.
Take a look at the following section of code.
var result=[]; function foo () { var i= 0 for(; i<3;i=i+1) {result[i]=// ////3
In this code, the programmer wants the variable i in the Foo function to be used by the inner loop function, and can get their index separately, and in fact, only the value that the variable is last reserved, that is. The free variable that is recorded in the closure is just a reference to the variable, not the value of the variable, when the variable is changed , the value of the variable obtained in the closure will also be changed.
One way to do this is to have the inner function execute as soon as the loop is created, capture the current index value, and then record it in one of its own local variables. Then, using the return function method, rewrite the intrinsic function, let the next call, return the value of the local variable, the improved code:
var result=[]; function foo () { var i= 0 for(; i<3;i=i+1) {result[i]= (functionfunction//// // 2
I'll explain here again. Here are 2 additional techniques that are called immediately by anonymous functions and return functions. It is also a difficult part for beginners to understand.
I understand the closure in JS