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

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)
主站蜘蛛池模板: 民县| 南城县| 靖宇县| 伊吾县| 沈阳市| 呼玛县| 普陀区| 永泰县| 濉溪县| 依安县| 临汾市| 嘉义市| 白银市| 云南省| 黎平县| 利川市| 龙海市| 北宁市| 红河县| 佛冈县| 丁青县| 金堂县| 澳门| 宝清县| 延庆县| 南涧| 新丰县| 安丘市| 大渡口区| 九江县| 平陆县| 永兴县| 丰原市| 普定县| 清水河县| 慈溪市| 荆门市| 门源| 诸暨市| 卢氏县| 额尔古纳市|