Lua Programming/How to Lua/colon

From Wikibooks, open books for an open world
< Lua Programming‎ | How to Lua
Jump to: navigation, search

Definition of methods[edit]

The colon symbol can be used for defining methods:

-- These two statements are equivalent
function a.b:c (params) body end
a.b.c = function (self, params) body end

Calls to object oriented methods[edit]

The colon symbol provides a special syntax for object oriented calls.

-- By using a colon operator, the name of the object does not need to be passed as a first argument
myobj:foo(n)    -- equivalent to  myobj.foo(myobj, n)

Calls to instance methods[edit]

The colon symbol is used to define calls to instance methods: