Skip to content

Instantly share code, notes, and snippets.

@sapier
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapier/11407336 to your computer and use it in GitHub Desktop.
Save sapier/11407336 to your computer and use it in GitHub Desktop.
----------------variant A
local function somefunc(self,a)
print("Have: " .. dump(self.have) .. " GOT: " .. a)
end
local sometable = {}
sometable.tablefunc = somefunc
sometable.have = "havesomething"
sometable:tablefunc("gettingsomething")
------------------variant b
local sometable = {}
function sometable:tablefunc(a)
print("Have: " .. dump(self.have) .. " GOT: " .. a)
end
sometable.have = "havesomething"
sometable:tablefunc("gettingsomething")
@ShadowNinja
Copy link

local someclass = {}
someclass.__index = someclass
function someclass:func(a)
    print("Have: "  .. dump(self.have) .. " GOT: " .. a)
end

local function new_someobject(have)
    local someobject = {}
    someobject.have = have
    return setmetatable(someobject, someclass)
end

local someobject = new_someobject("havesomething")
someobject:func("gettingsomething")

assert(someobject.func == new_someobject().func)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment