I wonder if there's a simpler way to call a function or set variable of the same table instead of writing the table name.
For example, Is there a simpler way to call MyClass.test()
function from MyClass.setup()
in my below example code?
local MyClass = {}
function MyClass.test()
print("Hello")
end
function MyClass.setup()
MyClass.test()
end
MyClass.setup()
If you use :
to call the functions instead of .
, Lua implicitly inserts a reference to the table itself as the first argument (similar to the this
pointer is some object-oriented languages). Then you can say self:test()
to get rid of the name dependence.
local MyClass = {
test = function(self)
print("Hello")
end,
setup = function(self)
self:test()
end
}
MyClass:setup()