Skip to content

Instantly share code, notes, and snippets.

@topin27
Created April 19, 2014 13:41
Show Gist options
  • Save topin27/11084775 to your computer and use it in GitHub Desktop.
Save topin27/11084775 to your computer and use it in GitHub Desktop.
--[[ 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