元表__index,以及闭包的调用结合使用
local newTable = {key1 = "value1"}
function newTable:cctv()
local mt = {}
mt.__index = function (table,key)
if(key == "key2") then
return function (obj,...) return obj:createNewView(...) end
end
return nil
end
setmetatable(self,mt)
end
function newTable:createNewView(...)
local p = {...}
print(p[1])
return self.key1
end
newTable:cctv()
print(newTable.key2) --> 只是函数的打印
print(newTable:key2("123")) -->这才是对函数内部闭包的调用
网友评论