Description of JavaScript anonymous Functions

Source: Internet
Author: User

1. What is an anonymous function?
There are three methods to define a function in javascript:

Function keyword statement:CopyCodeThe Code is as follows: function fnmethodname (x) {alert (x );}

Function literals ):Copy codeThe Code is as follows: var fnmethodname = function (x) {alert (x );}

Function () constructor:Copy codeThe Code is as follows: 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.Copy codeThe Code is as follows: 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.Copy codeThe Code is as follows: 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 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, and the browser reports a syntax error.Copy codeThe Code is as follows: function (){
Alert (1 );
}();

Function literal: first declare a function object and then execute it.Copy codeThe Code is as follows: (function (){
Alert (1 );
})();

Priority expression: Because the JavaScript Execution expression is from inside the parentheses to the outside, you can use parentheses to forcibly execute the declared function.Copy codeThe Code is as follows: (function (){
Alert (2 );
}());

Void Operator: Use the void operator to execute a separate operand without parentheses.Copy codeThe Code is as follows: 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.

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.