相关疑难解决方法(0)

函数/变量范围(按值或引用传递?)

我完全被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 …
Run Code Online (Sandbox Code Playgroud)

variables lua scope

31
推荐指数
4
解决办法
4万
查看次数

标签 统计

lua ×1

scope ×1

variables ×1