a.lua和b.lua在同一个目录下 a.lua调用b.lua中的test方法,注意b中test的写法 _M 和 a中调用方法: b.lua local _M = {}function _M.test() ngx.say("hello test!")endreturn _M a.lua local test = require("b")if not test then ngx.say("Failed to require b!")
本文是原创文章,如需转载,请注明文章出处 要用lua实现私有函数,关键就是使用metatable的特性来实现. Test.lua: local v = {};v.x = 100;v.y = 200; function v.new() local o = {}; setmetatable(o, v); local mt = {f=v.f,x=v.x,y=v.y}; v.__index = mt;--metatable中只提供f方法,则f成为共有函数,g成为私有函数 return o;end fun