Difference between colon (:) and dot (.) in Lua

Source: Internet
Author: User

In Lua development, we often confuse the differences between the two. Below is an example to explain:

1 class = {} 2 class. _ Index = Class 3 4 function class. new (x, y) 5 Local CLS ={} 6 setretriable (CLS, class) 7 Cls. X = x 8 Cls. y = y 9 return cls10 end11 function class: Test () 12 -- equivalent to 13 -- function class. test (Self) 14 print (self. x, self. y) 15 end16 17 18 object = Class. new (10, 20) 19 20 object: Test () 21 -- equivalent to 22 object. test (object)

You can see:
1. When defined: Class: Test () and class. test (Self) is equivalent, point number (.) to achieve the colon (:) effect, add a self parameter to the first parameter;
2. When calling: object: Test () is equivalent to object. Test (object). The vertex (.) must add the object itself to the first parameter.

Conclusion: You can view the dot (.) as a static method, and the colon (:) As a member method.

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.