Difference between ":" and "." in methods of a Lua table

5

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)?

    
asked by anonymous 08.12.2015 / 18:46

2 answers

7

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 .

    
08.12.2015 / 18:51
5

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.

    
08.12.2015 / 18:51