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

  • 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
主站蜘蛛池模板: 固原市| 黄山市| 嵊泗县| 定陶县| 棋牌| 道孚县| 安义县| 拉萨市| 高邮市| 区。| 锡林郭勒盟| 海淀区| 泰和县| 大邑县| 壤塘县| 甘德县| 庄浪县| 微博| 韩城市| 潼关县| 遂宁市| 宣恩县| 巫溪县| 罗定市| 万宁市| 乐至县| 芜湖市| 普陀区| 新蔡县| 稻城县| 登封市| 延寿县| 襄汾县| 嘉峪关市| 大竹县| 永丰县| 阿克苏市| 剑川县| 株洲县| 大城县| 上思县|