- 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)
推薦閱讀
- Dynamics 365 Application Development
- Arduino by Example
- R語言游戲數據分析與挖掘
- Mastering PHP Design Patterns
- Visual Basic程序設計教程
- 從0到1:Python數據分析
- 自然語言處理Python進階
- Getting Started with Gulp
- 51單片機C語言開發教程
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- 軟件測試教程
- Orchestrating Docker
- Getting Started with Python
- IoT Projects with Bluetooth Low Energy
- Scratch從入門到精通