--匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues
function newCounter()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end

c1 = newCounter()
print(c1())
print(c1())

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-06-19
  • 2021-11-20
  • 2021-07-19
  • 2022-12-23
猜你喜欢
  • 2021-05-21
  • 2021-11-26
  • 2021-04-02
  • 2022-12-23
相关资源
相似解决方案