1. Here are several forms of function calls:
The efficiency of various calls: in this article there is a talk about:
http://suqing.iteye.com/blog/1981591
Either of the following-patterns can be used to immediately invoke//a function expression, utilizing the function ' s execution context to//create "privacy." (function () {/* code */} ()); Crockford recommends this one (function () {/* code */}) (); But this one works just as well//Because the point of the the parens or coercing operators are to disambiguate//between FU Nction expressions and function declarations, they can be//omitted when the parser already expects a expression (but ple ASE See the//"Important note" below). var i = function () {return 10;} (), True && function () {/* code */} (); 0, function () {/* code */} ();//If You don't care about the return value, O R the possibility of making//your code slightly harder to read, you can save a byte by just prefixing//the function with A unary operator.! function () {/* code */} (); ~function () {/* code */} ();-function () {/* code */} (); +function () {/* code */} ();//Here's A Nother variation, from @kuvos-i ' m not sureOf the performance//implications, if any, of the using the ' new ' keyword, but it works.//http://twitter.com/kuvos/status/182 09252090847232new function () {/* code */}new function () {/* code */} ()//only need parens if passing arguments
2.What ' s wrong with "self-executing anonymous function?"
Such a function expression (iife), even if called, is not necessarily anonymous, so JavaScript community members this nickname "self-executing anonymous function" has a misleading place.
This is a self-executing function. It's a function that executes (or//invokes) itself, Recursively:function foo () {foo ();} This is a self-executing anonymous function. Because It has no//identifier, it must with the ' Arguments.callee ' property (which//specifies the currently Executin g function) to execute itself.var foo = function () {Arguments.callee (),};//this *might* be a self-executing anonymous F Unction, but only while the//' foo ' identifier actually references it. If you were to change ' foo ' to//something else, you ' d had a "Used-to-self-execute" anonymous function.var foo = function () {foo ();};//Some people call this a "self-executing anonymous function" Even though//it's not self-executing, Becaus E It doesn ' t invoke itself. It is//immediately invoked, however. (function () {/* code */} ());//Adding an identifier to a function expression (thus creating a named//function expression Can is extremely helpful when debugging. Once named,//However, the function is no longer anonymous. (function foo () {/* code */} ());//Iifes can also be self-executing, although this is, perhaps, not the most//useful pat Tern. (function () {Arguments.callee ();} ());(function foo () {foo ();} ());//One last thing to Note:this would cause an error in BlackBerry 5, because//inside a named function expression, tha T name is undefined. Awesome, huh? (function foo () {foo ();} ());
3.A Final aside:the Module Pattern
The module implementation pattern of JavaScript is to return an object instead of a function. Modularity has related methods and properties in a namespace, organizing the code of the entire module, reducing the pollution of global variables.
Here is an example:
Create an anonymous function expression, which gets invoked immediately,//and assign its *return value* to a variable. This approach "cuts out the//middleman" of the named ' Makewhatever ' function reference.////As explained in the above "I Mportant Note, "Even though Parens is not//required around this function expression, they should still is used as a//MA Tter of Convention to help clarify, the variable is being set to//the function ' s *result* and not the function itself . var counter = (function () {var i = 0; return {get:function () {return i; }, Set:function (val) {i = Val; }, Increment:function () {return ++i; } };} ());//' counter ' is a object with the properties, which in the happen to be//Methods.counter.get (); 0counter.set (3); counter.increment (); 4counter.increment (); 5COUNTER.I; Undefined (' I ' isn't a property of the returned object) I; REFERENCEERROR:I is not defined (it's only exists inside the closure)
Original reference: http://benalman.com/news/2010/11/immediately-invoked-function-expression/
A function expression that Javascript---immediately-invoked function expression (iife) immediately executes