- 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)
推薦閱讀
- Vue.js設計與實現
- The Android Game Developer's Handbook
- C# Programming Cookbook
- Network Automation Cookbook
- Python Geospatial Development(Second Edition)
- C語言程序設計上機指導與習題解答(第2版)
- Android玩家必備
- D3.js By Example
- AutoCAD 2009實訓指導
- C++ Fundamentals
- Oracle實用教程
- Learning Grunt
- SFML Game Development
- Learning Shiny
- Java 11 and 12:New Features