I'm going to propose a third path, to place both functions within a closure. It would look like:
var functionA = (function(){
function functionB() {
// do stuff...
}
function functionA() {
// do stuff...
functionB();
// do stuff...
}
return functionA;
})();
We create the closure by wrapping the declaration of both functions in a IIFE. The return value of the IIFE is the public function, stored in a variable of the name for the function. The public function can be invoked in exactly the same way as if it were declared as a global function, ie functionA()
. Note that the return value is the function, not a call to the function, thus no parens at the end.
By wrapping the two functions up like so, functionB
is now completely private, and cannot be accessed outside of the closure, but is visible only to functionA
. It is not cluttering the global namespace, and is not cluttering the definition of functionA
.