cocos2dx-lua中handler解析

先看一段代码:

local c=c or {}


function c:onTouch()
    print "test in onTouch"
end

function handler(obj, method)
    return function(...)
        return method(obj, ...)
    end
end

 

测试:

1:

print(handler(c,c.onTouch))
print(c.onTouch(c))

输出

function: 0x7f0d0884ae00                                                                                                
test in onTouch
2:
print(handler(c,c.onTouch))
print((function()c.onTouch(c)end))

输出

function: 0x7f44d8d2ab40                                                                                                
function: 0x7f44d8d29e30 
 
结论:
handler只不过是对method进行封装 套了一层匿名function并返回该匿名方法
所以handler(c,c.onTouch)()等价于c:onTouch()等价于c.onTouch(c)也同样等价于(function()c.onTouch(c)end())()

posted on 2017-04-26 15:34  益达的博客  阅读(930)  评论(0编辑  收藏  举报

导航