【发布时间】:2017-04-03 01:23:25
【问题描述】:
我需要检查一个成员是否存在于不在下一级但沿着成员路径的表中。
foo = {}
if foo.bar.joe then
print(foo.bar.joe)
end
这将转换为 attempt to index field 'bar' (a nil value),因为 bar 未定义。
我通常的解决方案是逐个测试链条。
foo = {}
if foo.bar and foo.bar.joe then
print(foo.bar.joe)
end
但是当有许多嵌套表时,这可能非常乏味。有没有比逐个测试更好的方法?
【问题讨论】: