首页 > 代码库 > lua闭包

lua闭包

function MakeCounter()    local t = 0    return function()        t = t + 1        return t    endendlocal func = MakeCounter()for i = 0,10 do    print(func())endlocal func2 = MakeCounter()for i = 0,10 do    print(func2())end

>lua -e "io.stdout:setvbuf ‘no‘" "main.lua"
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
>Exit code: 0

lua闭包