2 ms·
I think this is the only good reason for doing it the non-traditional way.
by ape4 13y ago
I think this is the only good reason for doing it the non-traditional way.
- windsurfer 13y 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.