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
文档信息
- 本文作者:zhupite
- 本文链接:https://zhupite.com/lua/lua-closure.html
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)