如何在可选的 lua 函数中传递表。
例如
function test(options)
local a = options.a
end
Run Code Online (Sandbox Code Playgroud)
这个功能应该同时工作
test(options)
Run Code Online (Sandbox Code Playgroud)
和
test()
Run Code Online (Sandbox Code Playgroud)
function test(options)
options = options or {}
local a = options.a or 0 -- or whatever it defaults to
end
Run Code Online (Sandbox Code Playgroud)
您只需or将可选值及其默认值即可。如果尚未提供该值,nil则它将解析为ored 值。
这是一个较短的版本
function test(options)
if not options then
options = {}
end
local a = 0
if options.a then
a = options.a
end
end
Run Code Online (Sandbox Code Playgroud)