模組定義 |
|
-- mymodule.lua local M = {} -- public interface -- private local x = 1 local function baz() print 'test' end function M.foo() print("foo", x) end function M.bar() M.foo() baz() print "bar" end return M -- Example usage: local MM = require 'mymodule' MM.bar()
這是一種常見的方法。它很簡單,不依賴外部程式碼,避免使用全域變數,而且陷阱很少。外部變數會加上「M.
」前綴,看得一清二楚。