Points, colons, and self in Lua

Source: Internet
Author: User

"points, colons, and self in Lua"

In LUA programming, the definition and invocation of functions are often encountered, sometimes called with a dot number, sometimes by colons.

Girl = {Money = $] Function girl.gotomarket (girl, Somemoney) Girl.money = Girl.money-somemoney End Girl.gotomarket (g IRL, +) print (Girl.money)

The method's point number definition and point number are called.

Boy = {Money = $] Function boy:gotomarket (somemoney) Self.money = Self.money-somemoney End Boy:gotomarket (+) PR Int (Boy.money)

A colon definition and a colon call are made above.


The above print results are all 100.

As you can see, the colon definition and the colon call are actually the same as the above effect, except that the first hidden argument is omitted, and the argument self points to the caller itself
We can also point a number to define a colon call, or a colon to define a point number to call
Such as:

Boy = {money = +} function Boy.gotomarket (self, somemoney) Self.money = Self.money-somemoney End Boy:gotomarket (10 0) Print (Boy.money)

Reference: Summary: The colon is just a function of omitting the first argument self, which points to the caller itself, and there is no other special place.

Points, colons, and self in Lua

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.