[removed] inline functions vs predefined functions

前端 未结 13 1856
心在旅途
心在旅途 2020-12-02 11:00

Can any body throw me some arguments for using inline functions against passing predefined function name to some handler.

I.e. which is better:

相关标签:
13条回答
  • 2020-12-02 11:37

    The predefined named functions can reduce the JavaScript callback hell issue, which is mentioned at http://callbackhell.com/

    0 讨论(0)
  • 2020-12-02 11:37

    I tend towards named functions as well. Anonymous function refs are quick, but should only be used for simple stuff. My rule of thumb is that if the function is more than 2 lines of code, it probably belongs in it's own definition.

    This is complicated by most example code making use of Anonymous functions. But the samples are usually very simplistic. The method falls apart as things get more complicated. I've seen function refs nested in function refs as the developer realized that more callbacks were needed in subsequent steps. Instead of this tree based logic, I prefer the organization of isolated functions.

    And usually end up happy that I can reuse one of the functions I define later.

    One important use of an Anonymous Function is when you need to pass scoped data into your function call, but then I usually just wrap my function into the anonymous function.

    Named functions are also absolutely necessary if you ever get into Test Driven Development.

    0 讨论(0)
  • 2020-12-02 11:39

    There is one significant difference between the two: The latter one has a name.

    I like to help my tools help me, and so I mostly avoid anonymous functions as my tools can't give me meaningful information about them (for instance, in a call stack list in a debugger, etc.). So I'd go with the

    (function(){
      function invokeMe() {
        /*code*/
      }
      setTimeout(invokeMe, 5);
    })();
    

    ...form in general. Rules are meant to be broken, though, not slavishly bowed to. :-)

    Note that according to the specification, there's a third alternative: You can have an inline function that also has a name:

    (function(){
      setTimeout(function invokeMe(){ /*some code here*/ }, 5);
    })();
    

    The problem, though, is that every version so far of the JavaScript interpreter from Microsoft ("JScript"), including (astonishingly) the one in IE9, handles that named function expression incorrectly and creates two completely distinct functions at different times. (Proof, try it in IE9 or earlier and also in just about any other browser.) IE gets it wrong in two ways: 1. It creates two separate function objects, and 2. As a consequence of one of those, it "bleeds" the name symbol into the enclosing scope of the expression (in clear violation of Section 13 of the specification). Details here: Double take

    0 讨论(0)
  • 2020-12-02 11:39

    IMO, declaring a function will be useful only if you intend to re-use it later, in some other way.

    I personally use function expressions (first way) for setTimeout handlers.

    However you might want to know the differences between function declarations and function expressions, I recommend you the following article:

    • Named function expressions demystified
    0 讨论(0)
  • 2020-12-02 11:40

    An inline function avoids namespace pollution and predefined functions have higher reuse. I think you could make cases where each is appropriate.

    0 讨论(0)
  • 2020-12-02 11:42

    In the example provided, the declaration and use of the function are so close that I think the only difference is readability. I prefer the second example.

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