Lua Basics: Use dot or colon when calling a function

Source: Internet
Author: User

This article was reproduced in: http://www.benmutou.com/archives/1789

1. Beginners most prone to confusion top1--call a function with a dot or colon?

Let's take a look at the following two lines of code:

Msprite.setposition (msprite:setposition);

For the first time a friend of Lua, this is a nightmare, for the function of the call there are two forms, let us casually pick the meaning of it?

The two forms are different, the difference is very large, but only one.

However, for the time being not explained, the following is introduced.

2. The simplest class

Let's take a look at the simple, to create a "class" try, the following code:

 tsprite = {x  = 0  , y  = 0  ,} function Tsprite.setpos        Ition (x, y) tsprite.x  = X;    Tsprite.y  = Y; End Tsprite.setposition ( 1 , 2  Span style= "color: #000000;"    >); Print (  tsprite coordinates ( " .. Tsprite.x.  " ,  .. Tsprite.y.  " )  ); 
It's actually creating a table that adds some fields to the table.

The output results are as follows:

[Lua-print] Tsprite coordinates (1,2)

Let's take a look at the SetPosition function, which actually invokes the X and Y fields through Tsprite.

And, the way we use setposition is to use the dot number, which is an authentic function call way, remember.

3. Can I use the real name? The role of--self

If you are more sensitive, you will find that the example is very problematic if we call it this way:

Local who = tsprite;     = Nil;       Who.setposition (12);

This will be an error, although through the Who can really successfully call the SetPosition function, but the function needs to use the tsprite, and at this time the tsprite is nil.

So, smart we can do this:

Tsprite ={x=0, y=0,} function Tsprite.setposition (self, x, y) self.x=x; Self.y=y; End Local who=Tsprite; Tsprite=Nil; Who.setposition (who,1,2); Print ("Tsprite Coordinates (".. Who.x.",".. Who.y.")");

The output is still:

[Lua-print] Tsprite coordinates (1,2)

Pay attention to the first parameter of SetPosition, we force the request to pass in a parameter, this parameter is tsprite itself.

So, when calling the SetPosition function, the content of the incoming who,who is tsprite content, so setposition can execute normally.

4. Play lazy traditional virtues-default self parameter, and default pass self parameter

If you let a high (Chao) (LAN) Ape man every time you create a function and call a function to handle self, then he will say, "Come here, I promise not to kill you."

So, LUA provides a new way to use it, yes, that's the colon .

Watch out, I'm talking about calling a function with a colon.

The following code:

Tsprite = {        0,        0,    }    function tsprite:setposition (x, y)         = x;         = y;    End    = tsprite;     = Nil;         Who:setposition (12);

First, note the definition of the SetPosition function, using a colon;

Second, note the invocation of the SetPosition function, using a colon.

The function of a colon is to add a hidden first argument to the function when the function is defined, and the current caller is passed in as the first argument by default when the function is called.

After using a colon, it is the same as when we just used the dot, but we no longer need to explicitly define the self parameter and actively pass the WHO parameter.

Well, that's the difference between a dot and a colon, and it can be said that the colon was born to make us lazy.

If you use cocos2d-x LUA for development, most of the cases use colons.

The reason is simple, because in most cases we use the self parameter, just like the This keyword in C + +.

5. End

The next article formally into the object-oriented content, I hope we have not forgotten the meta-table and meta-methods and other basic, object-oriented will be used.

Lua Basics: Use dot or colon when calling a function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.