Module Definition |
|
-- 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()
This is a common approach. It is simple, relies on no external code, avoids globals, and has few pitfalls. Externally facing variables are prefixed by "M.
" and clearly seen.