Lua upvalue definition
2014-07-23 12:20
489 查看
Lua is a lexically scoped language.The scope of a local variable begins at the first statement afterits declaration and lasts until the last non-void statementof the innermost block that includes the declaration.Consider the following example:
Notice that, in a declaration like
Because of the lexical scoping rules,local variables can be freely accessed by functionsdefined inside their scope.A local variable used by an inner function is calledan
upvalue, or external local variable,inside the inner function.
Notice that each execution of a local statementdefines new local variables.Consider the following example:
The loop creates ten closures(that is, ten instances of the anonymous function).Each of these closures uses a different
x = 10 -- global variable do -- new block local x = x -- new 'x', with value 10 print(x) --> 10 x = x+1 do -- another block local x = x+1 -- another 'x' print(x) --> 12 end print(x) --> 11 end print(x) --> 10 (the global one)
Notice that, in a declaration like
local x = x,the new
xbeing declared is not in scope yet,and so the second
xrefers to the outside variable.
Because of the lexical scoping rules,local variables can be freely accessed by functionsdefined inside their scope.A local variable used by an inner function is calledan
upvalue, or external local variable,inside the inner function.
Notice that each execution of a local statementdefines 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
yvariable,while all of them share the same
x.
相关文章推荐
- Lua的function、closure和upvalue
- Lua的function、closure和upvalue
- 关于lua的闭包(Closure)和Upvalue
- Lua的function、closure和upvalue
- Lua的function、closure和upvalue
- Lua upvalue
- Lua的upvalue和闭包
- lua __index __newindex upvalue 示例
- Lua的function、closure和upvalue
- Lua的function、closure和upvalue
- lua的全局变量、局部变量、函数闭包和非局部变量(upvalue)
- Lua的function、closure和upvalue
- Lua语法小贴士(十五)upvalue
- Lua的function、closure和upvalue
- Lua 学习笔记之 function、closure和upvalue
- lua upvalue
- lua 闭包及upvalue
- Lua的upvalue和闭包
- lua __index __newindex upvalue 示例
- Lua的function、closure和upvalue