Created
September 14, 2011 07:41
-
-
Save creationix/1216050 to your computer and use it in GitHub Desktop.
Pattern.js ported to Lua
This file contains hidden or 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
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