1、if 判断
lua中 nil 和 false 为 假,其余为真
2、table是否为空
local a = {}
正确是 if next(a) == nil then xx end 而不是 if a == {} then xx end
3、table长度
t = {1, 2, 3}
t[1] = nil
#t ==>3
table.remove(t, 1)
#t ==> 2
通过把元素设为 nil 无法改变 #table(除非最后一个元素),而table.remove则可以立即更新 #tabale
4、浮点数问题
math.floor(0.58*100) ==> 57
10 == 10.00000000000000001 ==> true
5、and、or
a and b or c (b须为真)
6、break、return
break、return 只能是一个块的最后一条语句或者是end、else、until前的一条语句
7、select函数
select(n, ...)返回变长参数中第n个参数
select("#", ...)返回变长参数的长度(含nil)
8、元方法
与算术类元方法不同,关系类元方法不能应用于混合类型
9、函数调用
先声明局部变量然后再定义,避免递归调用时调用全局变量(未定义)而产生错误
注意:变量仅在定义它的语句完成后才进入范围,如下是示例是错误的
local test = function(n) test(n-1) -- 此处调用test时,test定义没有完成 end