举个小例子,验证各种情况下lua的local变量的生效范围
local x = 10
local i = 1 --local to the chunk
while i <= x do
local x = i * 2 --local to the while body
print(x,"while中的X")
i = i + 1
end
for x=1,10 do
local x = x * 2 --local to the while body
print(x,"for循环下的x")
end
if i == 11 then
local x -- local to the "then" body
x = 20
print(x + 2,"if体内的x")
else
print(x,"if else下的x")
end
print(x,"代码chunk下的local x")
感兴趣可以运行一下或者更改一些条件,则可以了解lua下的local在if,while,for下的生效范围