【发布时间】:2011-09-01 22:59:43
【问题描述】:
我完全被 Lua 的变量范围和函数参数传递(值或引用)弄糊涂了。
请看下面的代码:
local a = 9 -- since it's define local, should not have func scope
local t = {4,6} -- since it's define local, should not have func scope
function moda(a)
a = 10 -- creates a global var?
end
function modt(t)
t[1] = 7 -- create a global var?
t[2] = 8
end
moda(a)
modt(t)
print(a) -- print 9 (function does not modify the parent variable)
print(t[1]..t[2]) -- print 78 (some how modt is modifying the parent t var)
因此,这种行为完全让我感到困惑。
这是否意味着表变量 被传递给函数 参考而不是价值?
-
全局变量是如何创建的 与已经定义的冲突 局部变量?
- 为什么
modt能够 修改表尚未moda无法 修改 a 变量?
- 为什么
【问题讨论】: