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
Stephen Bugs Kamenar
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
Colonel Thirty Two
source
to share
You can also do this:
t = {}
function t:a() print(self) end
+2
lhf
source
to share