您的位置:首页 > 编程语言 > Lua

Lua local function与function区别

2017-07-31 14:29 423 查看
 


Lua local function与function区别

标签: lua函数
2016-04-21 15:39 3736人阅读 评论(0) 收藏 举报


 分类:

Lua(3) 


版权声明:本文为博主原创文章,未经博主允许不得转载。

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
改为一下两种方式就可以正常运行
1 local function的声明放在引用的前面
local function test1()
print("hello test1")
end

function test()
test2()
test1()
end

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

test()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2 local function声明使用function方式
function test()
test2()
test1()
end

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

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

test()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: