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
相关文章推荐
- Lua local function与function区别
- Lua local function与function区别
- Lua中table的ipairs和pairs以及function的:和.区别
- function foo(){}、(function(){})、(function(){}())等函数区别分析
- jQuery中$(function()与(function($)等的区别详细讲解
- 浅谈c++ Closure与tr1::function区别
- SQL连接:localhost、127.0.0.1、(local)的区别
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- onclick="function()"和onclick="return function();"区别
- 内置系统账户:Local system/Network service/Local Service 区别
- cookie sessionStorage localStorage 区别
- Request.getLocalPort()和Request.getServerPort() 的区别
- Verilog基础知识0(`define、parameter、localparam三者的区别及举例)
- jquery与javascript中的两个页面加载事件 window.onload 与 $(document).ready(function(){})区别
- php中的header("Location:URL") 与 javascript中 window.localtion 的区别
- javascript两种function的定义介绍及区别说明
- local Vlan与end-to-end Vlan的区别
- lua5.2中对table.insert的区别
- calling c++ function from Lua, implement sleep function
- Lua 中 ipairs 与 pairs 的区别