I came across two different statements that left me confused.
obj = {}
function obj.Create(name)
end
function obj:GoGoGo(name)
end
What is the difference of the function declared with the .
(dot) and :
(colon)?
I came across two different statements that left me confused.
obj = {}
function obj.Create(name)
end
function obj:GoGoGo(name)
end
What is the difference of the function declared with the .
(dot) and :
(colon)?
The first is the member operator, it only separates who the role belongs to.
The second is a syntactic sugar, it is the same as:
function obj.GoGoGo(self, name)
end
Then it just puts one more parameter to get the object and give it a more OOP notation for the language. The same as many other languages make it a bit more automatic.
Call:
var x = obj
x.GoGoGo(10)
Actually the call will be:
obj.GoGoGo(x, 10)
To learn more about about the operation of the OOP Lua psu .
The "colon" is used to call self
as the first parameter of the function.
So obj:GoGoGo(name)
should be the same as obj.GoGoGo(obj, name)
.
I saw this in an OS response a while ago, but I did not find it.