Lua 函数 .. 可选表格

如何将可选的表格传递给 Lua 函数。

例如

function test(options)
   local a = options.a
end

此函数应该作为以下两种情况工作

test(options)

test()
点赞
用户2858170
用户2858170
# 将下面翻译成中文并且保留原本的 markdown 格式

function test(options)

options = options or {} local a = options.a or 0 -- or whatever it defaults to

end


你可以使用 `or` 将可选值与它们的默认值连接起来。如果值尚未提供,因此是 `nil`,则会解析为 `or` 连接的值。

这是以下代码的缩短版本

function test(options) if not options then options = {} end local a = 0 if options.a then a = options.a end end


2020-04-15 08:45:14