zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    数列分段 II
    Best Cow Fences
    愤怒的牛
    linux 查看文件
    糖果传递
    BL刀片更换主板设置raid
    glance启停
    depot制作
    刀片服务器密码过期, console无法登录解决方案
    DP无法删除失效的多路径链路方法
  • 原文地址:https://www.cnblogs.com/robinunix/p/7872561.html
Copyright © 2011-2022 走看看