从表中检索的函数引用不会接受任何参数。

我有一个函数引用的表格如下:

KLC.ChatCommandBank = {
test = KLC.TestFunction,
config = KLC.OpenInterfaceOptions,
option = KLC.OpenInterfaceOptions,
options = KLC.OpenInterfaceOptions,
help = KLC.PrintHelp
};

但是当 f = "test",并且 t 是一个字符串表格,我调用

KLC.ChatCommandBank[f](t);

然后函数

function KLC:TestFunction(tab)
    print(tab);
end

在调用函数时,tab 的值为 nil,尽管调用函数时 t 不是 nil

我怀疑这是由于函数引用表格没有定义参数引起的。我在谷歌上没有找到任何东西,并且我的尝试也无法修复它!欢迎任何建议。

点赞
用户1442917
用户1442917

这是因为当你将函数定义为 KLC:TestFunction(tab),它会有一个隐含的参数 self,它指向被调用的表。

当你使用 KLC.ChatCommandBank[f](t) 调用它时,你需要明确地传递一个参数来代替这个参数:

KLC.ChatCommandBank[f](KLC, t)

或者,你可以将定义改为 local function KLC.TestFunction(tab)

2013-06-18 03:19:04