Why am I zero? How should I do it?

t = {}
a = function() print(self) end
t.a = a
t:a()

      

I don't want to go through myself, I want to use syntactic sugar.

+3


source to share


2 answers


function foo:bar() ... end

quietly injects a parameter with a name self

as the first argument, making it equivalent function foo.bar(self) ... end

. This is not like Javascript, where there is a magic variable this

that just exists.

The second line should be:



a = function(self) print(self) end

      

+3


source


You can also do this:



t = {}
function t:a() print(self) end

      

+2


source







All Articles