Created
April 19, 2014 13:41
-
-
Save topin27/11084775 to your computer and use it in GitHub Desktop.
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
--[[ The nasty OO | |
Animal = { | |
size=1, age=2, | |
sleep = function (self) | |
print("I need sleep.") | |
print("size"..self.size.."\tage"..self.age) | |
end | |
} | |
Tiger = { | |
color=3, | |
run = function (self) | |
print("I can run. size="..self.size.."age="..self.age.."color="..self.color) | |
end | |
} | |
setmetatable(Tiger, {__index = Animal}) | |
t = Tiger | |
Tiger = nil | |
t:run() | |
]] | |
--[[ Lua OO | |
Animal = { | |
size = 1, | |
new = function (self, object) | |
object = object or {} | |
setmetatable(object, self) | |
self.__index = self | |
return object | |
end | |
} | |
cat = Animal:new() | |
print(cat.size) | |
]] | |
-- Javascript OO | |
function Person() | |
local name = 'default' | |
return { | |
get_name = function () | |
return name | |
end, | |
set_name = function (new_name) | |
name = new_name | |
end | |
} | |
end | |
p = Person() | |
print(p.get_name()) | |
p.set_name("topin27") | |
print(p.get_name()) | |
q = Person() | |
print(q.get_name()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment