function - Lua 函数 .. 可选表
问题描述
如何在可选的 lua 函数中传递表。
例如
function test(options)
local a = options.a
end
这个功能应该同时工作
test(options)
和
test()
解决方案
function test(options)
options = options or {}
local a = options.a or 0 -- or whatever it defaults to
end
您只需or
选择具有默认值的可选值。如果尚未提供该值,因此nil
它将解析为or
ed 值。
这是一个较短的版本
function test(options)
if not options then
options = {}
end
local a = 0
if options.a then
a = options.a
end
end
推荐阅读
- ios - Qt 5.12 for iOS:构建系统
- r - 查找 scale_x 或 y 连续的限制或避免删除 ggplot2 和 ggmap 中的行
- javascript - Javascript If 语句同时包含 AND ( && ) 和 OR ( || )
- python - ImportError:尝试相对导入超出顶级包
- sql - JSON数组到SQL表中的单行
- android - 在android中获取位置权限
- git - 将本地仓库连接到远程仓库上的新分支
- flutter - 如何将两个容器连续放置在颤动中?
- flutter - 有没有办法自定义一个重复的类?
- javascript - 跳过 createAsyncThunk 中的 arg 参数