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

10 lines
290 B
Lua

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