A Brief Talk on Lua Closure Related Content

                                     A Brief Talk on Lua Closure Related Content


table of Contents

1. Blog content

2. Content

What is a closure:

The role of closures:

3. Push

4. Conclusion


1. Blog content

This article is one of Lua's study notes. It is a brief talk about Lua closures. First of all, this article needs to have a basic understanding of Lua. As discussed in the previous article, bloggers generally learn how to use things first, and wait until Only when you are proficient will you go deeper into the meaning, so most of the articles posted by the blogger are about the usage rather than the very low-level things. The blogger also hopes to introduce its usage in a very simple and straightforward language. This article The blog is mainly used as a learning record.


2. Content

 

What is a closure:

We know that Lua, as a scripting language, is written in a very broad way. We can declare method variables anywhere, but the scope is different. What is a closure? As far as the blogger understands it, within the method The form of the embedded method can be called a closure, as shown below:

function outFunc()
	function insideFunc()
		-- body
	end
end

But this is not a complete form of closure. InsideFunc is the built-in function of outFunc. Normally we cannot call insideFunc externally. At this time, we can make a little change and use the built-in function insideFunc as the return of the external function outFunc. Value, we can get the embedded function by calling outFunc externally. This is the most basic content of the closure. The structure is as follows:

function outFunc()
	function insideFunc()
		--body
	end
	return insideFunc
end

 

The role of closures:

Above we know what a closure is, so now let’s take a look at what this closure can do. First, we declare a variable in the external outFunc and operate on the variable in the built-in function insideFunc. The structure is as follows :

function outFunc()
	local testNum = 1
	function insideFunc()
		testNum = testNum*10
		print("输出:"..testNum)
	end
	return insideFunc
end

First of all, we need to know one thing. Every time we call outFunc, we will declare a new testNum attribute and insideFunc method. Although the testNum and insideFunc generated by these different calls have the same name, they are independent of each other. For example, we call twice outFunc, and use two variables to accept the return value of outFunc, and then call these two variables to view the results, as shown below:

local func1 = outFunc()
local func2 = outFunc()


-------------------------调用并输入结果

print(func1())
输出:10
print(func1())
输出:100
print(func2())
输出:10
print(func1())
输出:1000

We use func1 and func2 to receive the result of two calls to outFunc, which is the built-in function insideFunc, then we call func1 twice, func2 once, and then func1 again. We found that after calling func1, he performed the internal testNum X10 operation, and save this record, when we call func1 next time, his internal testNum is the operation from the last recorded 10, and the result is 100, and then we call func2 to get the result 10, inside func2 The testNum will not be affected by func1, which proves its mutual independence.

Seeing this, I don’t know if anyone feels a sense of deja vu. Is the example right? Lua does not have the concept of object-oriented. In other languages ​​such as Java, C#, etc., we can instantiate an object. The new object has the same properties and methods as the original object. We can modify it. Now it seems that Lua closures and instances are similar to each other, so we can know that one of the functions of closures is to simulate object-oriented , The outer function is used to store variables and methods, and the internal variable method realizes the modification of internal variables.


3. Push

Github:https://github.com/KingSun5


4. Conclusion

        This blog is a brief discussion on closures. In the next blog, the blogger will use closures and implement the object-oriented process. Of course, simulating object-oriented is only one of the functions of closures. Everyone is welcome to supplement other closures. Function, if you think that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, please comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/105178445