![](https://csdnimg.cn/release/download_crawler_static/9821582/bgd.jpg)
print(x) --> 10 (the global one)
Notice that, in a declaration like local x = x, the new x being declared is not in scope yet, and so the
second
x refers to the outside variable.
Because of the lexical scoping rules, local variables can be freely accessed by functions defined inside their
scope. A local variable used by an inner function is called an upvalue, or external local variable, inside the
inner function.
Notice that each execution of a local statement defines new local variables. Consider the following
example:
a = {}
local x = 20
for i=1,10 do
local y = 0
a[i] = function () y=y+1; return x+y end
end
The loop creates ten closures (that is, ten instances of the anonymous function). Each of these closures
uses a different
y variable, while all of them share the same x.
2.7 - Error Handling
Because Lua is an embedded extension language, all Lua actions start from C code in the host program
calling a function from the Lua library (see
lua_pcall). Whenever an error occurs during Lua compilation or
execution, control returns to C, which can take appropriate measures (such as printing an error message).
Lua code can explicitly generate an error by calling the
error function. If you need to catch errors in Lua,
you can use the
pcall function.
2.8 - Metatables
Every value in Lua can have a metatable. This metatable is an ordinary Lua table that defines the behavior
of the original value under certain special operations. You can change several aspects of the behavior of
operations over a value by setting specific fields in its metatable. For instance, when a non-numeric value is
the operand of an addition, Lua checks for a function in the field
"__add" in its metatable. If it finds one, Lua
calls this function to perform the addition.
We call the keys in a metatable events and the values metamethods. In the previous example, the event is
"add" and the metamethod is the function that performs the addition.
You can query the metatable of any value through the
getmetatable function.
You can replace the metatable of tables through the
setmetatable function. You cannot change the
metatable of other types from Lua (except by using the debug library); you must use the C API for that.
Tables and full userdata have individual metatables (although multiple tables and userdata can share their
metatables). Values of all other types share one single metatable per type; that is, there is one single
metatable for all numbers, one for all strings, etc.
A metatable controls how an object behaves in arithmetic operations, order comparisons, concatenation,
length operation, and indexing. A metatable also can define a function to be called when a userdata is
garbage collected. For each of these operations Lua associates a specific key called an event. When Lua
performs one of these operations over a value, it checks whether this value has a metatable with the
corresponding event. If so, the value associated with that key (the metamethod) controls how Lua will
perform the operation.
Metatables control the operations listed next. Each operation is identified by its corresponding name. The
key for each operation is a string with its name prefixed by two underscores, '
__'; for instance, the key for
operation "add" is the string
"__add". The semantics of these operations is better explained by a Lua
function describing how the interpreter executes the operation.
页码,13
73(W)w
2011-01-07http://www.lua.or
/manual/5.1/manual.html