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

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)
主站蜘蛛池模板: 大埔区| 北安市| 罗平县| 五华县| 抚顺县| 江达县| 惠安县| 沿河| 鹿泉市| 邯郸市| 武威市| 罗江县| 九龙县| 荥阳市| 霸州市| 托克逊县| 湾仔区| 江津市| 浮梁县| 扬州市| 娄烦县| 沙雅县| 富阳市| 蓬莱市| 福清市| 松原市| 嵊泗县| 临泉县| 宝清县| 阜阳市| 曲水县| 本溪| 浙江省| 密山市| 临洮县| 韶山市| 昔阳县| 科尔| 六枝特区| 确山县| 卢湾区|