public
Created — forked from creationix/Pattern.lua

Pattern.js ported to Lua

  • Download Gist
Pattern.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Pattern = {}
 
function Pattern:extend(obj)
local child = obj or {}
setmetatable(child, {__index = self})
return child
end
 
function Pattern:new(...)
local obj = {}
setmetatable(obj, {__index = self})
if not self.initialize then
return obj
end
obj:initialize(unpack({...}))
return obj
end
Rectangle = Pattern:extend()
 
function Rectangle:initialize(width, height)
self.width = width
self.height = height
end
 
function Rectangle:getArea()
return self.width * self.height
end
 
local rect = Rectangle:new(10, 20)
print("rect " .. tostring(rect))
print("rect:getArea() " .. rect:getArea())
 
local MagicRectangle = Rectangle:extend()
 
function MagicRectangle:getArea()
return self.width * self.height * 100
end
 
local rect2 = MagicRectangle:new(10, 20)
print("rect2 " .. tostring(rect2))
print("rect2:getArea() " .. rect2:getArea())

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.