Module function malpractice?

Hello, I don’t necessarily have any problems but I want to know if there is a difference between:

moduleScript.function = function()
end

and

function moduleScript.function()
end

Is there any benefits and downsides to using one or the other?

1 Like

Nope, same thing. I just prefer the bottom method as I sometimes write my functions as methods (using :)

1 Like

I see, but in that case wouldn’t you get a self variable? How would it work?

The variable self is merely the table the method is in (if any).

local myModule = {key=true}

function myModule:func()
    print(self.key) -- true
end

You don’t have to use it, it just serves as a useful distinction as to what functions directly manipulate the table, for example, an OOP class (in which case you would have to use self).

local someClass = {}
someClass.__index = someClass

function someClass.new() -- . operator, indicating no changes to self
    
end

function someClass.is() -- . operator, indicating no changes to self

end

function someClass:increment() -- : operator, indicating some manipulation is happening with self

end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.