Lua系统学习01-DOFile加载函数

一、加载外部函数

dofile加载外部函数并执行

代码示例:

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Admin.
--- DateTime: 2021/11/8 13:49
---
print("hello world")
dofile("C:\\File\\Lua\\OutSideLua.lua")
print(twice(2))

外部函数 outSideLua

print("hello world")
function norm (x, y)
    local n2 = x^2 + y^2
    return math.sqrt(n2)
end
function twice (x)
    return 2*x
end

使用loadFile加载函数 loadFile(“路径”)

print("hello world")
f=loadfile("C:\\File\\Lua\\OutSideLua.lua")
f()
print(twice(2))

loadfile 加载函数 仅编译它把它以函数(可能类似委托)的形式当作返回值返回,但它不运行它。
当要去执行的时候,只要执行loadfile返回的函数即可。 这样跟DOFile是一样的,只是loadfile可以将加载和执行的时间段分开。
dofile加载函数 编译并执行,所以会得到chunk的执行结果。

参考文章:https://www.jb51.net/article/55125.htm

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/programming-life/lua/1806/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
Lua系统学习01-DOFile加载函数
一、加载外部函数 dofile加载外部函数并执行 代码示例: --- --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by Admin. --- DateTime:……
<<上一篇
下一篇>>
文章目录
关闭
目 录