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

Asked

Viewed 213 times

6

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 between the declared function and the . (point) and : (colon)?

  • The person who gave it -1 should be doing a hell of a lot of Lua ;)

  • 1

    Must be your fan :)

2 answers

7


The first is the member operator, it only separates to whom the function belongs.

The second is a syntactic sugar, is the same as:

function obj.GoGoGo(self, name)

end

Then it only puts one more parameter to receive the object and give a more OOP notation to the language. The same as so many other languages do a little more automatically.

Calling for:

var x = obj
x.GoGoGo(10)

In fact the call will be:

obj.GoGoGo(x, 10)

I put in the Github for future reference.

To know a little more on the functioning of the pseudo OOP of Lua.

5

The "two-point" serves to call self as the first function parameter.

Soon obj:GoGoGo(name) must be the same thing as obj.GoGoGo(obj, name).


I saw it in an OR response a while back, but I couldn’t find it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.