官术网_书友最值得收藏!

Scope access

Chunks are all about scope! You can access any variable defined outside of a scope from within the scope. Think of a scope like a one-way window in a room; from the inside you can see out, but from the outside you can't see in:

foo = 7 -- global scope
do
local bar = 8 -- local scope
print ("foo: " .. foo)
print ("bar: " .. bar)
end

However, you can't access a variable local to a scope outside of that scope:

foo = 7 -- global
do
local bar = 8 -- local
end
print ("foo: " .. foo)
print ("bar: " .. bar) -- error!
-- bar was declared local to the do/end chunk
-- it is trying to be printed at the file or
-- global chunk level, where it does not exist

The same access pattern is also true for multiple nested chunks:

foo = 7 -- global
do
local bar = 8 -- local
do
local x = 9 -- nested local
-- can access foo, bar and x
end
-- can access foo and bar
end
-- can only access foo
主站蜘蛛池模板: 霍山县| 化德县| 湘阴县| 南通市| 石泉县| 河东区| 凤阳县| 句容市| 镇原县| 闸北区| 宣汉县| 博客| 启东市| 铜川市| 红桥区| 二手房| 邮箱| 商洛市| 垫江县| 黄冈市| 正蓝旗| 莎车县| 南昌县| 漳浦县| 滦南县| 凤庆县| 东明县| 响水县| 孟村| 渝北区| 大石桥市| 彭阳县| 石门县| 玉田县| 罗定市| 本溪| 大余县| 赤峰市| 扶沟县| 和顺县| 洛宁县|