Marko
Marko

Reputation: 72232

Why are these functions assigned to objects with a "redundant" function name?

Please feel free to reword the title if you think you have a better one.

If you look through the source of bravo.js you'll notice the following on line 807.

module.declare = function main_module_declare(dependencies, moduleFactory)

What is the benefit of naming the function main_module_declare?

Couldn't that just be written as the following?

module.declare = function (dependencies, moduleFactory)

Upvotes: 0

Views: 79

Answers (3)

Wes
Wes

Reputation: 1090

I wrote that code, sorry for the late response LOL.

The reason is that I think functions without names are stupid and annoying to debug. There is nothing more annoying than a stack trace full of (anonymous) functions. @joseph-silber is right on the money, and "named function expression" is a good name for this pattern.

Upvotes: 0

hugomg
hugomg

Reputation: 69954

The biggest advantage of using an internal local variable for the function is that your module's code is indifferent as to whether the function is public or not. For example, if you want to turn a previously private function into a public function then just adding the module.publicName = privateName line is simpler then renaming all the intances of `privateName. This also applies the other way around if you want to remove a function from the public interface.

However, this doesn't apply to the particular situation you linked to, since the function isn't called again inside the module. That said, the comment mentions that module.declare could be overwritten so I guess there could be a reason for keeping a reference to the original function around somewhere.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

If you need to refer to the calling function in strict mode, it can be useful.

If you're like me and you don't give a toss for "stict mode", just use arguments.callee instead.

Upvotes: 4

Related Questions