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

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.


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

Search: