5 回答

慕虎7371278
TA贡献1802条经验 获得超4个赞
本身也是闭包的一个演示例子
首先你要看newCounter这个函数的返回值
它的返回值 返回的不是一个具体的数字,而是一个函数
所以 如果直接print这个函数 得到的是function xxxx
当调用这个返回的函数才能得到这个值

www说
TA贡献1775条经验 获得超8个赞
冒号本身是一个语法糖
当一个table内的方法被以冒号调用的话表示把这个table作为第一个参数传进去
写个demo
123456 | local t = {} t.fun = function(tab,v) print(tab,v) end t:fun(123) t.fun(t,123) |
这2个是相同的
所以根据你的function判断是否需要传入table来判断是否需要用冒号

慕盖茨4494581
TA贡献1850条经验 获得超11个赞

红糖糍粑
TA贡献1815条经验 获得超6个赞
function test(a, b)
return a+b, a-b
end
local m, n = test(5, 3)
print(m)
print(n)
输出结果为:
8
2

当年话下
TA贡献1890条经验 获得超9个赞
testFun是入口函数,也就是执行是从这个函数开始的,这个函数有两个参数,一个是表tab,一个是函数fun,它们都是形参;当调用它时会传入两个实参,它们的名称不一定和形参的名字相同。
f1是另一个函数,它是testFun函数中第二个参数的实参,也就是实际执行时是执行的这个函数
fun(k, v)在testFun传入实参后变成了f1(k, v)
- 5 回答
- 0 关注
- 441 浏览
添加回答
举报
0/150
提交
取消