Use of JavaScript anonymous functions: http://www.cnblogs.com/skykang/archive/2010/12/03/1895274.html
first, what is an anonymous function?
There are three ways of defining a function in javascript:
1. Function-Keyword statement:
function Fnmethodname (x) {alert (x);}
2. function literal (functions literals):
var fnmethodname = function (x) {alert (x);}
3. Function () constructor:
var fnmethodname = new Function (' x ', ' Alert (x); ')
The above three methods define the same method function Fnmethodname, the 1th is the most commonly used method, the latter two are to copy a function to the variable fnmethodname, and this function is no name, that is, anonymous function. In fact, quite a few languages have anonymous functions.
the difference between the function literal and the functions () constructor
1. Although the function literal is an anonymous function, the syntax allows you to specify any function name for it, and you can call it yourself when writing a recursive function, but not with the function () constructor.
var f = function fact (x) {if (x < = 1) return 1; else return x*fact (x-1);};
2. The function () constructor allows runtime JavaScript code to be created and compiled dynamically. In this way it resembles the global function eval ().
3. The function () constructor parses the functional body each time it executes and creates a new function object. Therefore, it is very inefficient to invoke the function () constructor in a loop or in a frequently executed functions. Instead, the function literal is not recompiled every time it is encountered.
4. Creating a function using the function () constructor does not follow a typical scope, it always treats it as a top-level function to execute.
var y = "global"; function constructfunction () {var y = "local"; return new Function ("Return y");//Cannot get local variable} alert (Constructfunction () () ); Output "global"
The function () constructor has its own characteristics and is much more difficult to use than the Functions keyword definition, so this technique is often seldom used. The function literal expression is very close to the definition of the function keyword. Consider the previous distinction, although there is a message that the literal anonymous function has bugs under some WebKit engines under OS X 10.4.3, but what we normally call anonymous functions refers to anonymous functions that take the form of function literals.
third, the code pattern of anonymous function
Code mode for anonymous functions: Error mode: It does not work, the browser will report grammatical errors.
function () {alert (1);} ();
1. Function literal: First declare a Function object, then execute it.
(function () {alert (1);}) ( );
2. Precedence expressions: Because the JavaScript execution expression is from inside the parentheses to the outside, you can enforce the declared function with parentheses.
(function () {alert (2);} ( ) );
3. Void operator: Use the void operator to execute a single operand that is not enclosed in parentheses.
void function () {alert (3);} ()
These three ways are equivalent, Hedger Wang for personal reasons prefer the 3rd kind, and in the actual application I see and use is the 1th kind.
iv. Application of anonymous functions
1. The first sentence in the JavaScript module model is that the "global variable is the Devil". With the VAR keyword, anonymous functions can effectively ensure that JavaScript is written on the page without polluting the global variables. This is very effective and graceful when adding JavaScript to a page that is not very familiar. In fact, Yui and its corresponding examples of the use of anonymous functions, and other JavaScript libraries are also a lot of use.
2. The cornerstone of JavaScript's functional programming (functional programming). See "Writing beautiful JavaScript with functional programming techniques" and "Functional JavaScript Programming Guide" for details.
Use of JavaScript anonymous functions