local lazy_tbl lazy_tbl = function(tbl, index) returnsetmetatable(tbl, { __index = function(self, key) for k,v inpairs(index) do print(k, v) end --print(key) for k,v inpairs(self) do print(k, v) end local fn = index[key] if fn then do --此处fn(self)和fn(),传入self形参与否的效果是一样,table中的匿名函数 --定义是没有参数的,这个形参是moonscript翻译后加上去的。 local res = fn(self) --local res = fn(self) local res = fn() self[key] = res --实际把return res去掉也不会影响这个程序的运行结果, 在此函数中 --从了这个return语句再也没有return调用了,而设定工作在self[key] = res这句就已经完成。 return res end end end }) end
local build_request build_request = function(unlazy) if unlazy == nilthen unlazy = false --unlazy = true end do local t = lazy_tbl({ }, ngx_req) if unlazy then for k inpairs(ngx_req) do --这里遍历的ngx_req,但是调用的函数是t的。 local _ = t[k] end end return t end end
local lazy_tbl lazy_tbl = function(tbl, index) returnsetmetatable(tbl, { __index = function(self, key) local fn = index[key] if fn then do local res = fn(self) self[key] = res return res end end end }) end