Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Lua is by design a very simple language. And self is the most ordinary variable, its peculiarity is that it is automatically created when using a colon in the declaration or calling functions (which are inside the table). And so self is the link to this table.
You can take this example:
local cat = {}
cat.name = 'Fluffy'
function cat:sayName()
print(self.name)
end
cat:sayName()
local cat = {}
cat['name'] = 'Fluffy'
cat['sayName'] = function(self)
print(self['name'])
end
cat:sayName()
local cat = {}
cat.name = 'Fluffy'
cat.sayName = function(self)
print(self.name)
end
cat:sayName()
local cat = {
name = 'Fluffy',
sayName = function(pet) -- не обязательно self
print(pet.name)
end
}
cat:sayName()
local cat
local name = 'Fluffy'
local sayName = function() -- вообще пусто
print(cat.name)
end
cat = {}
cat['name'] = name
cat['sayName'] = sayName
cat:sayName() -- Или просто cat.sayName() через точку
By analogy with other languages, this.
Read Chapter 16 "Object-Oriented Programming" in the book "Lua Programming" by Robert Yeruzalimsky (roottracker at your service).
Here is https://www.youtube.com/watch?v=F-xJq6s6lK0 video explaining the work of self (eng).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question