RosettaCodeData/Task/Call-an-object-method/Lua/call-an-object-method-2.lua

10 lines
290 B
Lua
Raw Permalink Normal View History

2013-06-05 21:47:54 +00:00
local methods = { }
function methods:func () -- if a function is declared using :, it is given an implicit 'self' parameter
print(self.name)
end
local object = setmetatable({ name = "foo" }, { __index = methods })
object:func() -- with : sugar
methods.func(object) -- without : sugar