2

例如,我有一个功能:

myFunction = function(a1,a2,a3)
end;

而且我想通过代码将 myFunction 给出的所有参数保存在其中,该代码在更改 myFunction 参数的数量及其名称后将是正确的。在我看来,它可以通过 for 循环来完成,但我不知道如何在其中调用 arguments 和#arguments。

4

1 回答 1

2
local saved_arguments

myFunction = function(...)
  -- save the arguments
  saved_arguments = {...}
  local a1, a2, a3 = ...
  -- main code of function
end;

-- Use saved arguments
local last_a1, last_a2, last_a3 = unpack(saved_arguments)
-- do something with last_a1, last_a2, last_a3
-- or use it directly: saved_arguments[1], saved_arguments[2], saved_arguments[3]
于 2013-03-12T10:59:59.037 回答