lua的逻辑运算: and or not
首先lua 中nil 和 false 才算是false,其他的都算是true ,0 也为true.
a and b , a 为false 时,返回a, 否则返回 b
a or b , a 为true 返回 a, 否则返回b
print(4 and 5) --输出 5
print(nil and 13) --输出 nil
print(false and 13) --输出 false
print(4 or 5) --输出 4
print(false or 5) --输出 5
在Lua中这是很有用的特性,也是比较令人混洧的特性。
我们可以模拟C语言中的语句:x = a? b : c,在Lua中,可以写成:x = a and b or c。
最有用的语句是: x = x or v,它相当于:if not x then x = v end 。
__index 为 table 的查询.
__newindex 为 table 的赋值.