Still confused about local and global

I am still confused about local and global.

I thought local variables and function where only accessible in the block that they are declared. So why does something like this work?

1
2
3
4
5
6
7
8
9
10
11
local disappear = function ()
        mo.alpha = 0
end     
 
local appear = function ()
        --Take .3 seconds to appear
        transition.to(mo, {time = 300,alpha = 1})
        --Disappear after .5 seconds
        timer.performWithDelay(moDisappearTime, disappear, 1)
end
<code>

locals are valid until the end of the block in which they are declared.

for example disappear is valid until the end of the file.

also dont forget that

local disappear = function() end

is the same as

local function disappear() end

There is a slight difference between the two function defines.

1
2
3
4
5
6
7
8
9
10
local x1 = function()
        print( "local disappear: " .. tostring(x1) )            -- prints "nil"
end
 
local function x2()
        print( "local disappear: " .. tostring(x2) )            -- prints the function address of x2
end
 
x1()
x2()

In short - you should try not to use

local x = function() end

and instead prefer

local function x() end

Dear akhtar,
Could you plz tell me why?

/*
In short - you should try not to use

local x = function() end

and instead prefer

local function x() end
*/

views:1310 update:2011/10/5 21:23:48
corona forums © 2003-2011