Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function class(constructor)
- namespace = {}
- namespace.__index = namespace
- namespace.new = function(...)
- local outerSelf = self
- -- aliases
- local this = {}
- self = this
- -- finish
- setmetatable(this,namespace)
- constructor(unpack(arg))
- self = outerSelf -- used to allow constructors inside constructors
- return this
- end
- return namespace
- end
- -- I COULD MAKE class(namespace,extend,constructor) but new function name is clearer
- function classExtends(extend,constructor)
- namespace = {}
- namespace.__index = namespace
- namespace.new = function(...)
- local outerSelf = self
- -- aliases
- local this = extend.new()
- self = this
- -- finish
- setmetatable(this,namespace)
- constructor(unpack(arg))
- self = outerSelf -- used to allow constructors inside constructors
- return this
- end
- return namespace
- end
- ----------
- -- TEST --
- ----------
- Test = class(function()
- self.a = 1
- self.b = 3
- function self:set_a(new_a)
- self.a = new_a
- end
- end)
- App = classExtends(Test, function(c)
- print(self.a, self.b, c)
- end)
- local o = App.new(6)
- o:set_a(2)
- print(o.a)
- App.new(7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement