Question:
Closures are supported by many languages, such as JavaScript and Lua. Kids who have some knowledge about closure functions may lament that closure functions seem to have done something that other common functions cannot do.
Common functions can directly read global variables. For example:
local n = 1function f1( ... ) return nendprint(f1()) --1
However, a common function cannot read a local variable in different scopes. For example:
function f1( ... ) local n = 1 return nendfunction f2( ... ) print(n)endf2() --nil
However, by using the following special function syntax, a function can read a local variable in a different scope than itself:
function f1( ... ) local n = 1 function f2( ... ) n = n + 1 return n end return f2endlocal result = f1()print(result()) --2
In the code above, function F2 is included in function F1. All local variables in function F1 are visible to F2. But in turn, the local variables in F2 are invisible to F1.
Since F2 can read local variables in F1, we can read internal variables outside F1 as long as F2 is taken as the return value!
This type of function is called a closure function.
Therefore, if we need to describe the white closure function in one sentence, it is: the function contains the subfunction, and finally returns the subfunction.
The greatest value of the closure function is that we can directly read the local variables of the function outside the function (that is, the subfunction.
After careful research, we will find that the F1 () function is like a "class", and its defined local variables are like the global variables of the "class", and the sub-function F2 () function, like this "class" method, you can directly use the global variable n of this "class. Magic?
Now I understand what a closure function is. Although its implementation is amazing, what is the use of a closure function?
1. cache: The most obvious benefit is data caching. We can set a variable that requires long-term use as a local variable of the closure function and use it directly in the subfunction. Therefore, the local variable is defined only once, but we can call the sub-function multiple times and use the variable. This method is more efficient than defining initialization variables in subfunctions. A common use of closure functions is that we can use this to implement the counting function. The closure function defines a counting variable, and the sub-function performs ++ operations on it. In this way, every time you call the closure function, the Count variable will add 1.
function f1( ... ) local n = 0 function f2( ... ) n = n + 1 return n end return f2endlocal count = f1()print(count()) --1print(count()) --2print(count()) --3print(count()) --4print(count()) --5
2. Implement encapsulation: As mentioned above, the closure function is like a "class". Only the methods in the closure function can use its local variables, methods other than closure functions cannot read their local variables. This achieves object-oriented encapsulation, making it safer and more reliable.
What is a closure function?