Lua closures

The closure function in Lua, if you want to call it outside, it must be called normally after the parent function of the closure is executed, otherwise the function reference is changed to null

_G.UIFather = {}

function UIFather:New(prefab)
    local t = { ["prefab"] = prefab }
    setmetatable(t, self)
    self.__index = self
    return t
end

function UIFather:RegisterCloseEvent(instance, func, ...)
	function UIFather:OnEvent(event,param)

	end
end

function UIFather:OnEnable(instance)
	
end

function UIFather:OnDisable(instance)
	
end


_G.A = _G.UIFather:New()

print("Before the mother function is called" ,A["OnEnable"], A["OnEvent"],UIFather["OnEvent"])

UIFather:RegisterCloseEvent()

print("After the mother function is called" ,A["OnEnable"], A["OnEvent"],UIFather["OnEvent"])


The result of the call is:
Before parent function call function: 0054CDF0 nil nil
After parent function call function: 0054CDF0 function: 0054CE70 function: 0054CE70
[Finished in 0.1s]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326095120&siteId=291194637