Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _M = {} -- module table
- _M.someProperty = 1 -- class properties
- local function createText()
- -- local function are still valid, but not seen from outside - "private"
- end
- local privateVar -- so do local variables
- _GLOBAL_VAR -- without local it's global
- function _M.staticMethod(vars)
- -- this is class method like function (dot)
- -- there is no "self"
- end
- function _M:someMethod(vars)
- -- this is object method like function (colon)
- -- there is "self"
- end
- function _M:newBaseObject()
- -- Here goes constructor code
- local object = display.newImage(...) -- could be a display object or an empty table {}
- object.vars = 'some vars'
- object.name = 'BaseObject'
- object.property = self.someProperty -- from module
- function object:sign(song)
- print(self.name .. ' is singing ' .. song)
- end
- function object:destroy()
- -- optional destructor, you can override removeSelf() as well
- self.vars = nil
- self:removeSelf()
- end
- return object
- end
- -- Now inheritance
- function _M:newChildObject()
- local object = _M:newBaseObject()
- -- override any methods or add new
- object.name = 'ChildObject'
- function object:tell(story)
- print(self.name .. ' is telling ' .. story)
- end
- return object
- end
- return _M -- return this table as a module to require()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement