三十一、Lua 函数回调技巧

三十一、Lua 函数回调技巧

技巧1:

1
2
3
4
5
6
7
8
local
a = {};
function
b()
print("Hello World")
end
a["sell"] = {callFunc =b}
a["sell"].callFunc()

技巧2:

使用lua 自带的 unpack :

解释:把一直数组(只有连续数字下标的 table)展开成一串返回值,但是对用字符串或别的东西做 key 的 table 无能为力。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function unpackex(tbl, args) 
local ret = {}
for _,v
in ipairs(args)
do
table.insert(ret, tbl[v])
end
return unpack(ret)
end
print(unpackex(
{one =
{"one", "two", "three"},
two = "T" , three = "TH"},
{"one", "two", "three"}
)
)

输出:>> table: 00ABC2D0TTH