Keep in mind that a:b(...)
and function a:b(...) ... end
is just a syntactic sugar. self
does not necessarily point to the "current object" because unlike in other programming languages, self
is just a variable and can be assigned to anything. See the example below for a demonstration:
function table:member(p1, p2)
print(self, p1, p2)
end
is just
table.member = function(self, p1, p2)
print(self, p1, p2)
end
and
table:member(1, 2)
is just
table.member(table, 1, 2)
hence
function table:member(self, p1, p2)
print(self, p1, p2)
end
table:member(1,2) --self=1 p1=2 p2=nil
because that's just
table.member = function(self, self, p1, p2)
print(self, p1, p2)
end
table.member(table, 1, 2) --self=1 p1=2 p2=nil