LUAAboutGlobal VariablesEnvironment is the content to be introduced in this article.Global VariablesFirst, let's take a look at the detailed description of this article.
I. environment of global variables
LUAWe have used an absolutely popular approach to implement global variables, reusing the existing table mechanism, and saving global variables in the _ G table. This is a lot better.
From the perspective of language implementation, you do not need to implement the global variables separately. You only need to reuse the table mechanism.
From the perspective of usage, although you are a global variable, it seems that you are a little overwhelmed by the local variable), but you are unfortunately placed in the table. So all the things that can be done for a table are the same for the global volume:
1. variable dynamic name access: during runtime, it is difficult to access the global volume. Now, because it is placed in the table, you only need to write _ G [varname] to access it, and write _ G [varname] = ABC to assign values. The chunk Code does not need to be concatenated with strings.
2. resumable can be used. Although resumable is also mentioned, it does not need to be mentioned here that the _ index and _ newindex operators are used for table Partitioning. By implementing these two operators, we can do a lot of things on the global variable _ G table, the most common of which is, implement _ index and _ newindex again to modify the rules for global variables in LUA, and add the rule "declare first and then use it without declaring that it is not usable" in the strong language. This is detailed in the manual and will not be copied.
Ii. Local Environment of global variables
In C ++, the scope of a variable is to look for it from the inside out, and it has a masking effect. InLuaIn terms of global variables, it is very interesting to have a feature that allows each function to have its own local "Global Environment". Listening to conflicts is actually not the case. "Local" refers to the internal function, and "Global Environment" refersGlobal VariablesThe _ G table to save. That is to say, LUA allows each function to use a private _ G table. In this article, this is called the function environment. Setfenv () can be used to change the environment of a specified function. The first parameter is the function name, and the second parameter is the private _ G name of the function. It can be initialized before being passed in. The first parameter can also be a number. 1 indicates the function currently being executed, and 2 indicates the caller of the current function, 3 indicates that the caller actually uses this number as the active function calling level at the top of the stack ).
Note that after setfenv () is called to set a private _ G table, the original _ G table is inaccessible by default, in this case, many functions in the original _ G cannot be used, such as print. Therefore, you can either save the original _ G table in the private _ G table, or set the _ index operator for the private _ G table to point to the original _ G table. In comparison, I prefer the latter, elegant and easy to use ......).
Ps. with emotion, table is the only complex structure in LUA. All complicated data structures and descriptions are based on this. The _ index and _ newindex operators are not one of the two very important retriable operators of table.
Summary:LUAAboutGlobal VariablesThe environment learning tutorial is complete. I hope this article will help you!