Function module.foo() vs module.foo = function()?

I usually use the form function module.foo() when declaring a public function in a moduleScript, but recently I stumbled upon the second form, module.foo = function().

I was wondering if there’s any difference at all between these two?

there’s a slight difference, but it’s down to personal choice really.

They do the same thing. The first one is more readable and looks nicer (at least to me, choose whichever based off of ur opinion), but they behave slightly different when you try to get information about them.

Using debug.info with the s option, you can view the function’s source. With module.foo = function(), this will expose the whole function’s source code. However, using function module.foo(), it will only expose the declaration line. Take for example an exploiter looking at ur code. It’s a bit more of a pain for them to have to decompile it to view it, but using debug.info(module.foo, "s") allows them to view it all with the first syntax.

please note that the name is still viewable for both (if you use debug.info with n) because it’s inside a table declaration. Any other function declaration, you can hide the name with the second syntax example.

function declaration

Normal function
image

Table function

Hope this helps!

2 Likes

Yes, there is a small difference.

local function foo()
    foo() -- works
end

is equivelant to

local foo
foo = function()
    foo() -- works
end

in both of these example foo is declared before it is assigns meaning you can call foo inside the foo function.

however if you do

local foo = function()
    foo() -- doesn't work
end

it can’t call itself

3 Likes