我试图理解lua中dofile()的使用。 将其放入 init.lua 中是一个很好的做法 归档一行包含函数声明、wifi 初始化等的其他文件? 初始化.lua:
dofile("InitWifi.lua")
dofile(helperfunctions.lua")
dofile(...) dofile(..)
tmr.alarm(0,3000, function()
runprogram()
end)
这将大大减少要测试的程序的下载时间。
dofile(xyx.lua) 的效果和 xyz.lua 的效果一样吗 代码包含在“myprogram.lua”文件中?
最佳答案
处理此主题的另一种方法是使用 require
函数。
require 的操作方式与 dofile 类似,它执行 Lua 文件,但它还内置了更多智能。
Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
require
是加载模块的常见做法,模块就像一个库。
将文件设置为模块的一个好处是,您可以通过在模块内定义范围来避免覆盖另一个文件中的函数。
模块foo.lua
的示例代码:
local foo {}
function foo.bar(a)
local a = x + 7
print(a)
end
-- More functions defined inside the foo table
-- ...
return foo
将 require 与模块一起使用的示例:
local f = require("foo")
f.bar(7)
关于lua - 调用文件nodemcu lua 8266,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878600/