Preface
The concept of closure exists in many languages. Here, I will analyze and summarize the concept of closure in Lua. I hope it will help you learn about Lua.
What is a closure?
A closure is a very important concept in Lua. A closure is a combination of a function and its reference environment. Let's take a look at the Code:
1234567891011 |
function newCounter() local i = 0 return Function () -- anonymous Function i = i + 1 return i end end c1 = newCounter() print(c1()) print(c1()) |
Based on the closure concept and the code above, we can talk about this concept. Closure = Function + reference environment. The newcounter function in the above Code returns a function, and the returned anonymous function is the function in the component of the closure; the referenced environment is the environment where variable I is located. Actually, the closure is just like a function in form and expression, but it is not actually a function. We all know that a function is a combination of some executable statements, these code statements are determined after the function is defined and will not change when executed. Therefore, the function has only one instance. The closure can have multiple instances at runtime. Different reference environments and the same function combination can generate different instances, just like the same class code, you can create different class instances. When reading other people's articles, we can see that subfunctions can use local variables in the parent function. This behavior is called a closure! This statement illustrates a form of closure, allowing us to better understand what a closure is. As for the deep closure, let's continue.
Look at the closure.
First, create a function in Lua, just like defining a normal type value. As mentioned in the previous article, there is no difference between a function in Lua and a normal type. A function in Lua is a so-called "first-class value". It can be stored in a variable or data structure and passed to another function as a parameter. It can be the return value of a function, it can also be created during running. The function in Lua is such a "thing", which is flexible. Is the concept of "non-local variables" mentioned in Lua functions? This is a very important concept. It can be understood as not a variable defined in the scope of local action. At the same time, it is not a global variable, that is, what we call upvalue, the existence of such a variable makes the closure in Lua complete. These variables are mainly used in nested functions and anonymous functions. We all know that You can redefine the function in Lua's function, that is, embedded function. Embedded functions can access all the local variables created by external functions, these variables are called the upvalue of the embedded function. upvalue actually refers to variables rather than values. These variables can be shared among internal functions, such as the following code:
123456789101112131415161718192021222324 |
function Fun1() local iVal = 10 -- upvalue Function innerfunc1 () -- embedded Function print(iVal) -- end Function innerfunc2 () -- embedded Function iVal = iVal + 10 end return InnerFunc1, InnerFunc2 end -- Assign a function to the variable. At this time, variable A is bound to the innerfunc1 and function B is bound to the innerfunc2 function. local a, b = Fun1() -- Call a() -->10 -- Call B B () --> modified upvalue ival in function B. -- Call a to print the modified upvalue a() -->20 |
The above simple code verifies that the built-in functions share upvalue, just like the member functions in the C ++ class can access and modify member variables.
Use closures
We can see that the closure is a combination of data and behavior, just like the class in C ++, so that the closure has good abstract capabilities. In some cases, we need to remember the status of data after a call is completed, just like static variables in C ++. After each call is completed, static variables are not cleared. You can use closure to complete this function. In the next blog, I will talk about using closure to complete the iterator function.
Summary
Closure is a very important concept. It can be understood or hard to understand. Simply put, closure is an embedded function and upvalue that can be accessed correctly. Many times, we understand this truth but do not use it. Therefore, we need to read more code, participate in more projects, and accumulate more project experience, to enrich your experience. At that time, the level of understanding will go up.
Http://cn.cocos2d-x.org/tutorial/show? Id = 1078