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

Shadowing

You can give a variable local to a chunk the same name as a global variable. If you were to do this, then print the variable inside the chunk, what would happen? The value of the variable inside the chunk would print.

This is called variable shadowing. If the same variable name is used in different scopes, the variable closest to the scope you are using it in will be used. The following code example demonstrates this concept:

message = "global-scope"
-- This should print: global-scope
print ("message: " .. message)
do
-- Shadow the message variable
local message = "local-scope"
-- This print uses the variable declared
-- in this block (shadowing). Should print: local-scope
print ("message: " .. message)
end
-- The variable that was declared in the local scope
-- of the above block is gone. message now holds
-- the global scope again. Should print: global-scope
print ("message: " .. message)
主站蜘蛛池模板: 团风县| 天祝| 齐齐哈尔市| 阳谷县| 卓资县| 满城县| 宜宾市| 双牌县| 土默特右旗| 潜山县| 故城县| 萝北县| 天水市| 竹溪县| 夏津县| 泽普县| 濮阳市| 玉屏| 库尔勒市| 大足县| 饶河县| 平凉市| 运城市| 海阳市| 南宁市| 祁东县| 奉贤区| 西青区| 介休市| 梅州市| 湘乡市| 华坪县| 日照市| 长汀县| 桂阳县| 平凉市| 蒙城县| 长宁区| 建平县| 邯郸市| 康马县|