我需要检查一个成员是否存在于一张表中,这张表不在下一个级别,而是沿着成员的路径。
foo = {}
if foo.bar.joe then
print(foo.bar.joe)
end
这会导致一个错误:尝试索引字段'bar'(一个空值)
,因为bar没有被定义。
我的常规解决方案是逐个测试链条的每个部分。
foo = {}
if foo.bar and foo.bar.joe then
print(foo.bar.joe)
end
但是,当有很多嵌套的表格时,这种方式可能非常繁琐。有没有比逐个部分测试更好的方法?