It's correct that self there can be changed to any name and still works - it's just a parameter name. It's not really a keyword.
To start explaining why it's often named self instead of something like the object type, lets consider another way to define the function: outside the table constructor.
function testButton.testEnabled(self)
self.enabled = not self.enabled
end
Here we could change self to anything other parameter name. However Lua provides a shorter way of writing the that section (method definition sugar syntax):
function testButton:testEnabled()
self.enabled = not self.enabled
end
This is equivalent to the first section. It has a first parameter named self, but it's just hidden. And that's really all that self is in Lua. I like this quote about it:
self is not a keyword. It's not magical. It's not managed by the language. It's just the name of the first argument passed to your function.
But why do people often still use self as the first parameter name in the 1st case, or the case you found? Here are three upsides to it:
- It makes it more obvious that it's for a method.
- It's consistent with method definition sugar syntax.
- It's short and doesn't require thinking of a new parameter name.
2 comments
STOP YOU ARE SO SMART YOU GOT 300 REP IN 2 hours DUDe!!! ;(
i'm not smart i'm just wasting too much time here.