How does naming an anonymous function in JavaScript make a difference?

后端 未结 4 616
北恋
北恋 2021-02-14 07:08

I am analyzing the following two urls from John Resig\'s site, but I am not understanding how giving a name to the anonymous function has made a difference.

My understan

相关标签:
4条回答
  • 2021-02-14 07:30

    In the first case, the yell method is trying to access ninja.yell, regardless of which object calls it. Whereas in the second, it tries to call yell which exists because the function is named.

    It is NOT a good example. A good example would use this.yell instead of ninja.yell, thus getting the yell method from the current object.

    0 讨论(0)
  • 2021-02-14 07:41

    in the examples internally you can skip the additional access to the ninja object in #13

    anonymous closure (accessing object ninja is needed although we are already in that context):

    var ninja = { 
      yell: function(n){ 
        return n > 0 ? ninja.yell(n-1) + "a" : "hiy"; 
      } 
    };
    

    named closure can be called directly:

    var ninja = { 
      yell: function yell(n){ 
        return n > 0 ? yell(n-1) + "a" : "hiy"; 
      } 
    };
    

    another advantage is that named closures enable stacktracing:

    so assuming you do:

    (function fooBar() { console.log(brazl); })();
    // will create an error with "fooBar" in the stack trace instead of "anonymous function"
    

    EDIT: although it might look like overhead sometimes it helps debugging during development and for example YUICompressor and Closure Compiler can strip these names if they are not essentially needed

    0 讨论(0)
  • 2021-02-14 07:45

    The site http://kangax.github.com/nfe/ is a great reference. Yes, as far as it is a function expression the name will only be available inside (e.g. for recursive calls, as in the demonstration) and also helps for debugging (e.g. in stack traces), because it sets the name property of the function.

    0 讨论(0)
  • 2021-02-14 07:46

    Not trying to be combative with Kolink, but he goes a bit too far in saying it is NOT a good example. What #14 has to do with (in the links you shared) are named function expressions (a different animal from function declarations). Regardlesss of where the function reference is passed, if you name your function expression, it will always have a way to call itself, from within itself. This name, that you give your function expression, is a name that only it knows; it does not exist in any external scope.

    See here and here on MDN, for a further discussion about function expressions vs. function declarations. The second link, at the bottom, has a heading about named function expressions. It does have a use; see my Gist for an example of one-off recursive function, that adds nothing to the local or global variable scope (useful for one-off DOM traversal, for instance).

    Also, Tobias (in his answer here) points out other good uses of named function expressions, namely, in debugging.

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