Difference between . and : in Lua

Jason S picture Jason S · Feb 6, 2011 · Viewed 38k times · Source

I am confused about the difference between function calls via . and via :

> x = {foo = function(a,b) return a end, bar = function(a,b) return b end, }
> return x.foo(3,4)
3
> return x.bar(3,4)
4
> return x:foo(3,4)
table: 0x10a120
> return x:bar(3,4)
3

What is the : doing ?

Answer

BMitch picture BMitch · Feb 6, 2011

The colon is for implementing methods that pass self as the first parameter. So x:bar(3,4)should be the same as x.bar(x,3,4).