What's the contrast between . and?

What’s the difference between using “.” and “:” ? I’ve never really noticed a difference.
For example:

function module.randomfunction()
function module:randomfunction()

module.randomfunction()
module:randomfunction()

a:b(...) is syntactic sugar for a.b(a, ...). Defining this function with a colon will implicitly pass the table the function was used from as self. This is predominantly used in object oriented programming (OOP):

local Class = {}
Class.__index = Class

function Class.new(name)
    return setmetatable({ name = name }, Class)
end

function Class:GetName()
    return self.name
end

-- the function can otherwise be defined as
function Class.GetName(self)
    return self.name
end

local class1 = Class.new("Class1")
local class2 = Class.new("Class2")

print( class1:GetName() ) --> "Class1"
print( class1.GetName(class1) ) --> "Class1"

print( class2:GetName() ) --> "Class2"
print( class2.GetName(class2) ) --> "Class2"
3 Likes

Adding on to above, knowing this you can do things like

local text = "HELLO"

print(string.lower(text))

print(text:lower())

both will return the lowercase “hello”

4 Likes

i also go into : and . in this video a little that also might help

1 Like