What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?

前端 未结 5 1614
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 06:51

I\'m new-ish to JavaScript. I understand many of the concepts of the language, I\'ve been reading up on the prototype inheritance model, and I\'m whetting my whistle with m

相关标签:
5条回答
  • 2020-12-02 06:58

    Its more readable using named functions and they are also capable of self-referencing as in the example below.

    (function recursion(iteration){
        if (iteration > 0) {
          console.log(iteration);
          recursion(--iteration);
        } else {
          console.log('done');
        }
    })(20);
    
    console.log('recursion defined? ' + (typeof recursion === 'function'));
    

    http://jsfiddle.net/Yq2WD/

    This is nice when you want to have an immediately invoked function that references itself but does not add to the global namespace. It's still readable but not polluting. Have your cake and eat it to.

    Hi, my name is Jason OR hi, my name is ???? you pick.

    0 讨论(0)
  • 2020-12-02 07:02

    I use anonymous functions for three reasons:

    1. If no name is needed because the function is only ever called in one place, then why add a name to whatever namespace you're in.
    2. Anonymous functions are declared inline and inline functions have advantages in that they can access variables in the parent scopes. Yes, you can put a name on an anonymous function, but that's usually pointless if it's declared inline. So inline has a significant advantage and if you're doing inline, there's little reason to put a name on it.
    3. The code seems more self-contained and readable when handlers are defined right inside the code that's calling them. You can read the code in almost sequential fashion rather than having to go find the function with that name.

    I do try to avoid deep nesting of anonymous functions because that can be hairy to understand and read. Usually when that happens, there's a better way to structure the code (sometimes with a loop, sometimes with a data table, etc...) and named functions isn't usually the solution there either.

    I guess I'd add that if a callback starts to get more than about 15-20 lines long and it doesn't need direct access to variables in the parent scope, I would be tempted to give it a name and break it out into it's own named function declared elsewhere. There is definitely a readability point here where a non-trivial function that gets long is just more maintainable if it's put in its own named unit. But, most callbacks I end up with are not that long and I find it more readable to keep them inline.

    0 讨论(0)
  • 2020-12-02 07:04

    I prefer named functions myself, but for me it comes down to one question:

    Will I use this function anywhere else?

    If the answer is yes, I name/define it. If not, pass it as an anonymous function.

    If you only use it once, it doesn't make sense to crowd the global namespace with it. In today's complex front-ends, the number of named functions that could have been anonymous grows quickly (easily over 1000 on really intricate designs), resulting in (relatively) large performance gains by preferring anonymous functions.

    However, code maintainability is also extremely important. Each situation is different. If you're not writing a lot of these functions to begin with, there's no harm in doing it either way. It's really up to your preference.

    Another note about names. Getting in the habit of defining long names will really hurt your file size. Take the following example.

    Assume both of these functions do the same thing:

    function addTimes(time1, time2)
    {
        // return time1 + time2;
    }
    
    function addTwoTimesIn24HourFormat(time1, time2)
    {
        // return time1 + time2;
    }
    

    The second tells you exactly what it does in the name. The first is more ambiguous. However, there are 17 characters of difference in the name. Say the function is called 8 times throughout the code, that's 153 extra bytes your code didn't need to have. Not colossal, but if it's a habit, extrapolating that to 10s or even 100s of functions will easily mean a few KB of difference in the download.

    Again however, maintainability needs to be weighed against the benefits of performance. This is the pain of dealing with a scripted language.

    0 讨论(0)
  • 2020-12-02 07:15

    Well, just to be clear for the sake of my arguments, the following are all anonymous functions/function expressions in my book:

    var x = function(){ alert('hi'); },
    
    indexOfHandyMethods = {
       hi: function(){ alert('hi'); },
       high: function(){
           buyPotatoChips();
           playBobMarley();
       }
    };
    
    someObject.someEventListenerHandlerAssigner( function(e){
        if(e.doIt === true){ doStuff(e.someId); }
    } );
    
    (function namedButAnon(){ alert('name visible internally only'); })()
    

    Pros:

    • It can reduce a bit of cruft, particularly in recursive functions (where you could (should actually since arguments.callee is deprecated) still use a named reference per the last example internally), and makes it clear the function only ever fires in this one place.

    • Code legibility win: in the example of the object literal with anon funcs assigned as methods, it would be silly to add more places to hunt and peck for logic in your code when the whole point of that object literal is to plop some related functionality in the same conveniently referenced spot. When declaring public methods in a constructor, however, I do tend to define labeled functions inline and then assign as references of this.sameFuncName. It lets me use the same methods internally without the 'this.' cruft and makes order of definition a non-concern when they call each other.

    • Useful for avoiding needless global namespace pollution - internal namespaces, however, shouldn't ever be that broadly filled or handled by multiple teams simultaneously so that argument seems a bit silly to me.

    • I agree with the inline callbacks when setting short event handlers. It's silly to have to hunt for a 1-5 line function, especially since with JS and function hoisting, the definitions could end up anywhere, not even within the same file. This could happen by accident without breaking anything and no, you don't always have control of that stuff. Events always result in a callback function being fired. There's no reason to add more links to the chain of names you need to scan through just to reverse engineer simple event-handlers in a large codebase and the stack trace concern can be addressed by abstracting event triggers themselves into methods that log useful info when debug mode is on and fire the triggers. I'm actually starting to build entire interfaces this way.

    • Useful when you WANT the order of function definition to matter. Sometimes you want to be certain a default function is what you think it is until a certain point in the code where it's okay to redefine it. Or you want breakage to be more obvious when dependencies get shuffled.

    Cons:

    • Anon functions can't take advantage of function hoisting. This is a major difference. I tend to take heavy advantage of hoisting to define my own explicitly named funcs and object constructors towards the bottom and get to the object definition and main-loop type stuff right up at the top. I find it makes the code easier to read when you name your vars well and get a broad view of what's going on before ctrl-Fing for details only when they matter to you. Hoisting can also be a huge benefit in heavily event-driven interfaces where imposing a strict order of what's available when can bite you in the butt. Hoisting has its own caveats (like circular reference potential) but it is a very useful tool for organizing and making code legible when used right.

    • Legibility/Debug. Absolutely they get used way too heavily at times and it can make debug and code legibility a hassle. Codebases that rely heavily on JQ, for instance, can be a serious PITA to read and debug if you don't encapsulate the near-inevitable anon-heavy and massively overloaded args of the $ soup in a sensible way. JQuery's hover method for instance, is a classic example of over-use of anon funcs when you drop two anon funcs into it, since it's easy for a first-timer to assume it's a standard event listener assignment method rather than one method overloaded to assign handlers for one or two events. $(this).hover(onMouseOver, onMouseOut) is a lot more clear than two anon funcs.

    0 讨论(0)
  • 2020-12-02 07:19

    A bit late to the party, but some not yet mentioned aspects to functions, anonymous or otherwise...

    Anon funcs are not easily referred to in humanoid conversations about code, amongst a team. E.g., "Joe, could you explain what the algorithm does, within that function. ... Which one? The 17th anonymous function within the fooApp function. ... No, not that one! The 17th one!"

    Anon funcs are anonymous to the debugger as well. (duh!) Therefore, the debugger stack trace will generally just show a question mark or similar, making it less useful when you have set multiple breakpoints. You hit the breakpoint, but find yourself scrolling the debug window up/down to figure out where the hell you are in your program, because hey, question mark function just doesn't do it!

    Concerns about polluting the global namespace are valid, but easily remedied by naming your functions as nodes within your own root object, like "myFooApp.happyFunc = function ( ... ) { ... }; ".

    Functions that are available in the global namespace, or as nodes in your root object like above, can be invoked from the debugger directly, during development and debug. E.g., at the console command line, do "myFooApp.happyFunc(42)". This is an extremely powerful ability that does not exist (natively) in compiled programming languages. Try that with an anon func.

    Anon funcs can be made more readable by assigning them to a var, and then passing the var as the callback (instead of inlining). E.g.: var funky = function ( ... ) { ... }; jQuery('#otis').click(funky);

    Using the above approach, you could potentially group several anon funcs at the top of the parental func, then below that, the meat of sequential statements becomes much tighter grouped, and easier to read.

    0 讨论(0)
提交回复
热议问题