Created
August 11, 2014 13:57
-
-
Save svanellewee/94d03599eb2fad087050 to your computer and use it in GitHub Desktop.
Lua inheritance via metatables. Pretty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function __define_class(base) | |
local __classmethods = {} -- define static/classmethods here.. | |
function __classmethods.new ( cls, ...) -- classmethod | |
local self = setmetatable({}, { __index = cls }) | |
if self.init then | |
self:init(...) | |
end | |
return self | |
end | |
base = base or {} | |
local new_subclass = setmetatable(__classmethods, { __index = base }) | |
return new_subclass | |
end | |
local BaseAppClass = __define_class() | |
function BaseAppClass:init() | |
end | |
local AppClass = __define_class(BaseAppClass) | |
function AppClass:init(name, css_class) | |
BaseAppClass.init(self) | |
-- some specifics! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment