This article details the closures in Javascript. This article details the closures in Javascript.
I. Closure !?
Closure is a difficult point in the Javascript language and is not easy for beginners to understand. Let's take a look at the meaning of the closure.
Baidu encyclopedia and "official" explanation: the so-called "closure" refers to an expression (usually a function) with many variables and an environment bound to these variables ), therefore, these variables are part of the expression.
Wikipedia: In programming languages, closures (also known as lexical closures or function closures) are used to bind lexical range names in languages with the first type of functions.
However, we can see that these official explanations do not clearly understand what a closure is, but only have a question mark, "What are you talking about ?".
var a=50;function fun1(){ alert(a);}fun();
In the above Code, a is defined outside the function, so calling variable a inside the function can naturally read its value. Here, we call a global variable.
function fun2(){ var a=50;}alert(a);
In this Code, we define a inside the function, so when calling a outside the function, it will report an error, telling you that "a is not defined ", can't we find? Have we defined? Of course! It is just a local variable.
3. How to Find the variable "hidden" in the function
When we press the keyboard code, there will always be some reasons, and we need to get the local variables inside the function, then we need to extract the local variables stored in the function in a way.
That is, within the function, we define another function.
Function fun3 () {var a = 50; function fun4 () {return a;} return fun4 ();} // console. log (a); error console. log (fun3 ());
In the above Code, we defined a variable a in fun3, so that we can call a outside function fun3, and we will also report an error and cannot find it, for function fun4 within function fun3, function a can be called. Because function a is outside function fun4, fun3 cannot be called if function fun4 defines a variable.
Another concept is involved here: chain scope, that is, scope chain. The scope chain means that the same variable name will generate a scope chain. when accessing the variable name, it will first find whether the local scope contains the variable. If not, it will go to the parent scope to find the variable, if the global variable cannot be found, an error is reported.
If you want to call variable a outside function fun3, You need to define function fun4 in function fun3. Let's let it return the variable a that can be called, then the result of function fun4 is variable a, and then function fun4 is returned. Then, when function fun3 is executed, the result is the value of variable.
Simply put, As long as fun4 is used as the return value, we can read its internal variables outside fun3.
4. What should I pay attention to when calling local variables (using closures?
1. The closure will store the variables in the function in the memory, which consumes a lot of memory. Therefore, the closure cannot be abused. Otherwise, the web page performance will decrease.
2. Do not change the internal variable value of the parent function.
Finally, the closure is a function that can read the internal variables of other functions, so that we can call the variables in the parent function outside the parent function, it is not difficult to understand the code. The important thing is that in the unpredictable code, the specific application of closure still needs to be fully understood before it can be skillfully applied.
The above is a detailed description of the closure in Javascript. For more information, see other related articles in the first PHP community!