3 ms·
Have you ever worked in a large Javascript codebase? Anonymous functions allow for namespacing. Declare your namespace as var Namespace = {}, and then attach fu
by windsurfer 12y ago
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(){}.
- Roboprog 12y agoI'm rather fond of that style, or the variant: projectname = { A_CONSTANT: ..., someFunc: function(...) { }, ... lastFunc: function(...) { } } Basically, JSON layout but with the functions in the resulting namespace. Debugging & diagnostics for this layout in Firefox and Chrome is not a problem. As for IE, well, some things can't be fixed, I guess.
- sopooneo 12y agoI do that a bit, but found it tricky to end everything with a comma, and effectively use ":" instead of "=" for declarations. Lately I've been doing projectname = (function(){ self = {}; self.A_CONSTANT = ...; self.someFunc = function(...){ }; self.lastFunc = function(...){ }; return self; })(); I think that produces the exact same end result, but someone set me straight if I'm mistaken.
- Roboprog 12y agoWhat 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.
- ape4 12y agoI think this is the only good reason for doing it the non-traditional way.
- windsurfer 12y agoOh, you'd like more? How about simple closures: 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: var inCart = function(){ $(self) .addClass('disabled') .text('In Cart'); }; if (Cart.isMixIdInCart(results['mixId'])){ inCart(); }else{ $('.widget .purchase').on('click', function(){ $('.widget .purchase').text('Adding'); Cart.addToCart(results['mixId']).done(inCart); return false; }); } These are simply different ways to use closures, but that's part of the benefit of using anonymous functions.