Have you ever worked in a large Javascript codebase? Anonymous functions allow for namespacing. Declare your namespace as var Namespace = {}, and then attach functions as Namespace.foo = function(){}. Typically in a definition file I will have var self = Namespace; and then declare functions as self.foo = function(){}.
What you do is a bit more complicated, but has the benefit of making your data "private" within a closure. You are using a function as an object, and assigning other functions as fields within its "prototype". I think these inner functions can call each other w/out a prefix, as methods of the same object.
My version is the "static method" version that requires the namespace and a dot before any function calls. I'm OK with that, unless I need a bunch of instances of something.
P.S. - the semicolons don't really do anything (other than make Doug C feel good), as JS is a line oriented language, and will complete statements when they "look done", semis or not. Think of semicolons in JS like colons in BASIC: they let you put a second statement on the same line.
function getCounter(){
var num = 0;
return function(){
return num++;
};
}
How about overriding default functions? This is a snippet from a codebase I'm working on right now:
require(['helpers/user'], function(User){
// hack because we are not using layout.master
User.isLoggedIn = function(){ return global.userInfo.id > 0; };
});
How about re-using some code in a function? Another snippet from the same code, where self can change based on context: