【发布时间】:2016-04-26 15:58:14
【问题描述】:
我有以下功能
function test()
local function test2()
print(a)
end
local a = 1
test2()
end
test()
这打印出 nil
以下脚本
local a = 1
function test()
local function test2()
print(a)
end
test2()
end
test()
打印出 1。
我不明白这一点。我认为声明一个局部变量使其在整个块中有效。既然变量 'a' 是在 test()-function 范围内声明的,而 test2()--function 是在同一个范围内声明的,为什么 test2() 不能访问 test() 局部变量?
【问题讨论】:
-
lua 不是 javascript,它不会“提升”变量。
标签: scope lua local-variables