Skip to content

Instantly share code, notes, and snippets.

@creationix
Created September 14, 2011 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save creationix/1216050 to your computer and use it in GitHub Desktop.
Save creationix/1216050 to your computer and use it in GitHub Desktop.
Pattern.js ported to Lua
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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment