最近看leader写的代码, 里面有一句 cc({}), 不大理解。
于是断点进去, 跟到了framework\cc\init.lua中。
local GameObject = cc.GameObject
local ccmt = {}
ccmt.__call = function(self, target)
if target then
return GameObject.extend(target)
end
printError("cc() - invalid target")
end
setmetatable(cc, ccmt)
也就是说 cc({}) 其实就是调用table的元方法 __call
lua的 __call是这么定义的: sets handler h(f, …) for function call (using the object as a function )
大概的意思就是给cc设置回调函数, 当使用cc()时触发。