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

  • Lua Quick Start Guide
  • Gabor Szauer
  • 177字
  • 2021-08-05 10:30:38

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
主站蜘蛛池模板: 五华县| 邛崃市| 东丽区| 昌都县| 山丹县| 定兴县| 兴隆县| 延津县| 宜都市| 宜兰县| 奉节县| 新邵县| 阿拉善左旗| 济南市| 太湖县| 昌吉市| 石狮市| 安龙县| 额济纳旗| 双柏县| 廊坊市| 扬中市| 德保县| 富裕县| 明溪县| 陇川县| 闸北区| 京山县| 吴忠市| 梁山县| 万荣县| 松溪县| 上思县| 林周县| 望谟县| 黄陵县| 通渭县| 凉城县| 和龙市| 邵阳县| 临清市|