Whats the difference from doing ":" and "." for a custom function?

Some people in functions use : while others use . and some people do both. What’s the difference?

Using colons is syntatic sugar to the following:

local A = {}

function A.Do(self, ...)
    local The_A_Table = self
    -- do stuff
end

A.Do(A, ...)

In which can be done the same with lesser code:

local A = {}

function A:Do(...)
    local The_A_Table = self -- automatically supplied
    -- do stuff
end

A:Do(...) -- no need to send the table.
3 Likes

What’s self? and how does it connect to tables?

i use . for constructing objects and i use : for methods of the object

--OOP
local module ={}
module.__index = module

--Constructor
function module.new()
  local self = setmetatable({}, module)
  return self
end

--Methods
function module:Foo()
 --do stuff
end

this might help