Lua函数范围

Lua函数范围,lua,Lua,如果我这样做,我会出错。我该怎么办 local function one() local function two() local function three() callMe() -- got error here end end end local function callMe() print ("can't call :(") end callMe() 正如巴特·基尔斯所说,调用three()会出错,

如果我这样做,我会出错。我该怎么办

local function one()
    local function two()
        local function three()
            callMe() -- got error here
        end
    end
end

local function callMe()
    print ("can't call :(")
end

callMe()

正如巴特·基尔斯所说,调用
three()
会出错,因为
callMe
three
范围之外的本地函数,所以它不知道该函数。

以及缺少的
()
对于
one
two
three
,正如巴特·基尔斯所说,调用
three()
会出错,因为
callMe
three
范围之外的一个局部函数,所以它不知道该函数。

局部函数在使用之前必须声明:

local callMe
local function one()
    local function two()
        local function three()
            callMe() -- got error here
        end
    end
end
function callMe()
    print ("can't call :(")
end
callMe()

使用前必须声明本地人:

local callMe
local function one()
    local function two()
        local function three()
            callMe() -- got error here
        end
    end
end
function callMe()
    print ("can't call :(")
end
callMe()

抱歉,该代码无效,Lua:函数
1
2
3
后面需要
()
。很抱歉我的疏忽。我在这里写示例代码时很匆忙,但那不是真正的代码。好吧,那代码是无效的Lua:函数
1
2
3
需要
()
。很抱歉我的疏忽。我在这里编写示例代码时很匆忙,但这不是真正的示例代码。