Lua local function与function区别

1 使用function声明的函数为全局函数,在被引用时可以不会因为声明的顺序而找不到
2 使用local function声明的函数为局部函数,在引用的时候必须要在声明的函数后面

例子:
下面这段代码会报函数找不到的错误:lua: test.lua:3: attempt to call global ‘test1’ (a nil value)

function test()
    test2()
    test1()
end

local function test1()
    print("hello test1")
end

function test2()
    print("hello test2")
end

test()

改为一下两种方式就可以正常运行

1 local function的声明放在引用的前面

local function test1()
    print("hello test1")
end

function test()
    test2()
    test1()
end

function test2()
    print("hello test2")
end

test()

2 local function声明使用function方式

function test()
    test2()
    test1()
end

function test1()
    print("hello test1")
end

function test2()
    print("hello test2")
end

test()
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值