Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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(){}.


I'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.


I 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.


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.


I think this is the only good reason for doing it the non-traditional way.


Oh, 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.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: