Javascript anonymous function summary. For more information, see. 1. What is an anonymous function?
There are three methods to define a function in Javascript:
- Function keyword (function) Statement:
function fnMethodName(x){alert(x);}
- Function Literals):
var fnMethodName = function(x){alert(x);}
- Function () constructor:
var fnMethodName = new Function('x','alert(x);')
The above three methods define the same method function fnMethodName. The 1st method is the most commonly used method. The last two methods copy a function to the variable fnMethodName, which has no name, that is, anonymous functions. In fact, many languages have anonymous functions.
Ii. Differences between Function literal and Function () constructor
- Although a Function is an anonymous Function, the syntax allows you to specify any Function name for the Function. When writing a recursive Function, you can call it yourself. If you use Function () to construct a Function, you cannot.
var f = function fact(x) { if (x < = 1) return 1; else return x*fact(x-1);};
- Function () constructor allows dynamic creation and compilation of Javascript code during runtime. In this way, it is similar to the global function eval ().
- Function () constructor parses the Function body each time it is executed and creates a new Function object. Therefore, it is very inefficient to call Function () constructor in a loop or frequently executed Function. On the contrary, function literal is not re-compiled every time.
- When using Function () to construct a Function, it does not follow the typical scope. It always treats it as a top-level Function for execution.
Var y = "global"; function constructFunction () {var y = "local"; return new Function ("return y ");// Unable to obtain local variables} Alert (constructFunction ()());// Output "global"
Compared with Function keyword definition, the Function () constructor has many features and is difficult to use. Therefore, this technology is rarely used. The function literal expression is very similar to the function keyword definition. Considering the difference above, although there are bugs in some webkit engines under OS X 10.4.3 for anonymous functions with literally sent messages, however, the anonymous functions we usually call all refer to anonymous functions in the form of functions literally. For more details, see The Functions chapter in JavaScript: The Definitive Guide, 5th Edition.
Iii. Code mode of anonymous Functions
Yesterday, hedger wang introduced several code modes for anonymous functions in his blog:
Error Mode: It cannot work. The browser reports a syntax error.
function(){ alert(1);}();
- Function literal: First declare a function object and then execute it.
(function(){ alert(1);} ) ( );
- Priority expression: Since the Javascript Execution expression is from the inside of the parentheses to the outside, you can use parentheses to force the declaration function.
( function(){ alert(2);} ( ) );
- Void Operator: Use the void operator to execute a separate operand without parentheses.
void function(){ alert(3);}()
The three methods are equivalent. hedger wang prefers 3rd for personal reasons, and I see 1st in actual applications.
Iv. Application of anonymous Functions
- The first sentence in Javascript's module mode is "global variables are the Devil ". With the var keyword, the anonymous function can effectively ensure that Javascript is written on the page without causing global variable pollution. This is very effective and elegant when adding Javascript to a page that is not very familiar. In fact, YUI and its corresponding examples use a lot of anonymous functions, and many other Javascript libraries also use them.
- The cornerstone of functional programming in Javascript. For details, see compiling beautiful JavaScript with functional programming technology and functional JavaScript programming guide.