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)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163