Why do you need to invoke an anonymous function on the same line?

前端 未结 19 1537
走了就别回头了
走了就别回头了 2020-11-22 00:17

I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works - everytime I was just told to use it...:

//          


        
19条回答
  •  时光取名叫无心
    2020-11-22 00:51

    1. Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

      Anonymous functions are declared using the function operator instead of the function declaration. You can use the function operator to create a new function wherever it’s valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.

      Here’s a typical example of a named function:

      function flyToTheMoon() { alert("Zoom! Zoom! Zoom!"); } flyToTheMoon(); Here’s the same example created as an anonymous function:

      var flyToTheMoon = function() { alert("Zoom! Zoom! Zoom!"); } flyToTheMoon();

      For details please read here:

      http://helephant.com/2008/08/23/javascript-anonymous-functions/

提交回复
热议问题