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

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)
主站蜘蛛池模板: 永城市| 高青县| 河北区| 太和县| 神池县| 中西区| 平凉市| 盐边县| 颍上县| 本溪| 古田县| 镇平县| 陵川县| 获嘉县| 县级市| 吉林省| 汝州市| 淮滨县| 阳曲县| 南昌县| 五台县| 广西| 大英县| 扎囊县| 定结县| 合水县| 麦盖提县| 磐石市| 巩留县| 昭通市| 梓潼县| 陈巴尔虎旗| 赫章县| 七台河市| 建宁县| 长春市| 略阳县| 大邑县| 巴林右旗| 曲松县| 西城区|