朱皮特的博客 自由的飞翔

lua-closure

2013-03-17
朱皮特
lua
阅读量:

function  newCounter()
    local  i = 0
    return function ()      -- anonymous function
        i = i + 1return i
    end
end

c1 = newCounter()
print(c1())  --> 1print(c1())  --> 2
c2 = newCounter()
print(c2())  --> 1print(c2())  --> 2print(c1())  --> 3

下一篇 lua-regex

Comments

Content