lua - 调用文件nodemcu lua 8266

标签 lua esp8266 nodemcu

我试图理解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 文件,但它还内置了更多智能。

Lua require

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/

相关文章:

Lua 尝试调用 nil

function - Lua:冒号、 'self' 和函数定义与调用

Lua脚本没有按顺序执行

MySQL 代理逐步配置

Lua 和序列化闭包

c++ - Arduino WiFi ESP8266 条件日志数据失败

jquery - 如何通过ESP8266模块将网页数据发送到Arduino UNO?

c - 如何使用 NodeMCU 和 enduser_setup 捕获用户的 MAC 地址?

使用 Micropython 和 NodeMCU 12E 的 I2C 扫描返回空列表