JavaScript Anonymous function Explanation _ Experience Exchange

Source: Internet
Author: User
One, what is an anonymous function?
There are generally three ways to define a function in javascript:

Functions keyword (function) statement:
Copy Code code as follows:

function Fnmethodname (x) {alert (x);}

Functions literal (function literals):
Copy Code code as follows:

var fnmethodname = function (x) {alert (x);}

function () constructor:
Copy Code code as follows:

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 not a name, that is, anonymous function. In fact, quite a few languages have anonymous functions.

the difference between function literals and functions () constructors
Although the function literal is an anonymous function, the syntax allows you to specify any function name, and you can call itself when you write a recursive function, which is not possible with the function () constructor.
Copy Code code as follows:

var f = function fact (x) {
if (x < = 1) return 1;
else return x*fact (x-1);
};

The function () constructor allows run-time JavaScript code to be dynamically created and compiled. In this way it resembles the global function eval ().
The function () constructor resolves the body of the functions every time it executes and creates a new function object. Therefore, it is very inefficient to call function () constructors in a loop or in frequently executed functions. Instead, the literal volume of a function is not recompiled every time it is encountered.
When a function () constructor is created, it does not follow a typical scope, and it is performed as a top-level function.
Copy Code code as follows:

var y = "global";
function Constructfunction () {
var y = "local";
Return the new Function ("Return y"); Unable to get local variable
}
Alert (Constructfunction () ()); Output "global"

The function () constructor has its own characteristics and is more difficult to use than the functional keyword definition, so this technique is often rarely 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 our usual anonymous function refers to an anonymous function that takes the form of a function literal. More details can be read in the functions chapter of the Javascript:the definitive Guide, 5th Edition.

code patterns for anonymous functions
Yesterday Hedger Wang introduced several code patterns for anonymous functions in his blog:

Error mode: It does not work, the browser will report syntax errors.
Copy Code code as follows:

function () {
Alert (1);
}();

function literal: Declare a Function object first, and then execute it.
Copy Code code as follows:

(function () {
Alert (1);
} ) ( );

Precedence expression: Because the JavaScript execution expression is from inside the parentheses to the outside, the declared function can be enforced with parentheses.
Copy Code code as follows:

(function () {
Alert (2);
} ( ) );

void operator: A void operator is used to perform a single operand that is not surrounded by parentheses.
Copy Code code as follows:

void function () {
Alert (3);
}()

These three ways are equivalent, Hedger Wang for personal reasons like the 3rd kind, and in the actual application I see and use is the 1th kind.
iv. Application of anonymous function

The first sentence of a modular pattern of JavaScript is "global variable is the Devil". With the VAR keyword, anonymous functions can effectively ensure that JavaScript is written on the page without causing global variables to be contaminated. 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 a large number of use of anonymous functions, other JavaScript libraries are also a large number of uses.
The cornerstone of JavaScript's functional programming (functional programming). See "Using functional programming techniques to write graceful JavaScript" and "Functional JavaScript Programming Guide."

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.