When to use . and : in table functions?

I’m into modules and OOP, but i’m in doubt. When should i use ., when should i use :? I don’t know what’s the difference between them, can someone explain?

function module.foo()
--stuff
end
function module:foo()
--stuff
end

. Is used when you’re not referring something inside a table(like a variable). : is when you want to use the self keyword to refer the table’s one of it’s content.

2 Likes

You can use both, though, the difference it’s when using : self gets automatically asigned.

function someObject.Method(self)
  print(self)
end

function someObject:Method()
  print(self) -- self is automatically defined
end

Other example;

local someObject = {
  p= print
}

someObject.p("Test") --> "Test"
someObject:p("Test") --> table, "Test"
2 Likes