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

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
主站蜘蛛池模板: 桑植县| 南安市| 潮安县| 西充县| 普兰店市| 焉耆| 潼关县| 枣庄市| 阿城市| 甘洛县| 阜阳市| 乐都县| 炎陵县| 柳林县| 驻马店市| 承德县| 新化县| 宜州市| 景泰县| 武夷山市| 乌兰县| 玛沁县| 桦南县| 岑溪市| 云安县| 绿春县| 滦南县| 宁河县| 娱乐| 巴林右旗| 冀州市| 临江市| 读书| 宕昌县| 古浪县| 惠东县| 北辰区| 那坡县| 扶沟县| 奉化市| 江西省|