[关闭]
@zero1036 2017-04-12T07:40:30.000000Z 字数 716 阅读 1155

LuaScript语法技巧

Redis


方法

方法定义

  1. local function decreaseChance()
  2. end

传参

  1. local function lower(top)
  2. result = top + 10;
  3. end
  4. lower(20);

自定义function优先级

local定义方法应优先定义,否则报错:

  1. ERR Error running script (call to f_6a61b7950435646d00aaa5a535e2c3a0b9d356ec): @enable_strict_lua:15: user_script:6: Script attempted to access
  1. --错误示例
  2. if limit<=3 then
  3. lower(limit);
  4. else
  5. upper();
  6. end
  7. local function upper()
  8. ...
  9. end
  10. local function lower(top)
  11. ...
  12. end
  1. --正确示例
  2. local function upper()
  3. ...
  4. end
  5. local function lower(top)
  6. ...
  7. end
  8. if limit<=3 then
  9. lower(limit);
  10. else
  11. upper();
  12. end

循环

类型转换

tonumber (e [, base])

功能:尝试将参数e转换为数字,当不能转换时返回nil
base(2~36)指出参数e当前使用的进制,默认为10进制,如tonumber(11,2)=3

tostirng(e)

功能:将参数e转换为字符串,此函数将会触发元表的__tostring事件

type(v)

功能:返回参数的类型名("nil","number", "string", "boolean", "table", "function", "thread", "userdata")

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注