- Lua Quick Start Guide
- Gabor Szauer
- 149字
- 2021-08-05 10:30:39
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)
推薦閱讀
- Design Principles for Process:driven Architectures Using Oracle BPM and SOA Suite 12c
- Django開發從入門到實踐
- Python自動化運維快速入門
- 21天學通C++(第6版)
- 信息技術應用基礎
- 數據結構習題解析與實驗指導
- 軟件品質之完美管理:實戰經典
- Python算法詳解
- 軟件測試綜合技術
- Learning Nessus for Penetration Testing
- Python數據可視化之美:專業圖表繪制指南(全彩)
- 算法設計與分析:基于C++編程語言的描述
- 大學計算機基礎實訓教程
- AMP:Building Accelerated Mobile Pages
- SEO教程:搜索引擎優化入門與進階(第3版)