Javascript anonymous Functions

Source: Internet
Author: User

1. What is an anonymous function?
There are three methods to define a function in Javascript:
1. function keyword statement:
Function fnMethodName (x) {alert (x );}

2. Function 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 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
1. Although the Function literal is an anonymous Function, the syntax allows you to specify any Function name for it. When writing a recursive Function, you can call it yourself by using Function () the constructor does not work.
Var f = function fact (x ){
If (x <= 1) return 1;
Else return x * fact (x-1 );
};

2. Function () constructor allows dynamic creation and compilation of Javascript code during runtime. In this way, it is similar to the global function eval ().

3. The Function () constructor parses the Function body each time it executes 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.

4. When using Function () to construct a Function to create 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"); // The local variable cannot be obtained.
}
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 anonymous functions in the OS that call the literal number of messages
Some webkit engines under X 10.4.3 have bugs, but the anonymous functions we call usually refer to anonymous functions in the form of functions literally. For more details, refer to The Functions chapter on "JavaScript: The Definitive Guide, 5th Edition" recommended by netizens.

Iii. Code mode of anonymous Functions

Yesterday, hedger wang introduced several code modes for anonymous functions in his blog:
Error Mode: it cannot work, and the browser reports a syntax error.
Function (){
Alert (1 );
}();

1. Function literal: first declare a function object and then execute it.
(Function (){
Alert (1 );
})();

2. Priority expression: Since the Javascript Execution expression is from inside the parentheses to the outside, you can use parentheses to forcibly execute the declared function.
(Function (){
Alert (2 );
}());

3. 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
1. 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.

2. The cornerstone of Javascript functional programming (functional programming. For details, see compiling beautiful JavaScript with functional programming technology and functional JavaScript programming guide.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.