简体   繁体   English

如何在 lua 表中调用未命名的 function? [从桌子外面]

[英]How to call unnamed function in a lua table? [from outside the table]

p={update=function(} print("hello") end} p={update=function(} print("hello") end}

I can call the function within p using p.update() outside the table.我可以在表外使用 p.update() 在 p 内调用 function 。

How do I call unnamed functions though?我如何调用未命名的函数? ie IE

p={function(} print("hello") end} p={function(} print("hello") end}

If I know the index of the function, 1 in this case, can I use that in my function call?如果我知道 function 的索引,在这种情况下为 1,我可以在 function 调用中使用它吗? like p1?像p1? I've tried some variations of this to no success.我已经尝试了一些变化,但没有成功。

Yes.是的。 p.update is just syntactic sugar for one-word string keys. p.update只是一个单词字符串键的语法糖。

The more general way to index a table is to put the key value in brackets:索引表的更通用方法是将键值放在括号中:

p[1]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM