代码拉取完成,页面将自动刷新
-----------------------------------------------------------------------------------
-- Invoker.lua
-- Enrique García ( enrique.garcia.cota [AT] gmail [DOT] com ) - 4 Mar 2010
-- Helper function that simplifies method invocation via method names or functions
-----------------------------------------------------------------------------------
--[[ Usage:
require 'middleclass' -- or similar
require 'middleclass-extras.init' -- or 'middleclass-extras'
MyClass = class('MyClass')
MyClass:include(Invoker)
function MyClass:foo(x,y) print('foo executed with params', x, y) end
local obj = MyClass:new()
obj:invoke('foo', 1,2) -- foo executed with params 1 2
obj:invoke( function(self, x, y)
print('nameless function executed with params', x, y)
, 3, 4) -- nameless function executed with params 3, 4
Notes:
* The function first parameter must allways be self
* You can use Invoker independently: Invoker.invoke(obj, 'method')
]]
assert(Object~=nil and class~=nil, 'MiddleClass not detected. Please require it before using Beholder')
local function _invokeString(self, methodName, ...)
local method = self[methodName]
assert(type(method)=='function', 'Could not find ' .. methodName .. ' in ' .. tostring(self))
return method(self, ...)
end
local function _invokeFunction(self, func, ...)
return func(self, ...)
end
local _functionByType = { ['string'] = _invokeString, ['function'] = _invokeFunction }
Invoker = {
invoke = function(self, methodOrName, ...)
local f = _functionByType[type(methodOrName)]
if f then return f(self, methodOrName, ...) end
error('methodOrName should be either a function or string. It was a '.. type(methodOrName) .. ': ' .. tostring(methodOrName))
end
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。