JavaScript Anonymous function Explanation _ Experience Exchange
Source: Internet
Author: User
First, what is an anonymous function? There are three ways of defining a function in javascript:
Function-Keyword statement:
Copy CodeThe code is as follows:
function Fnmethodname (x) {alert (x);}
function literal (functions 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 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 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.
Copy CodeThe code is as follows:
var f = function fact (x) {
if (x < = 1) return 1;
else return x*fact (x-1);
};
The function () constructor allows runtime JavaScript code to be created and compiled dynamically. In this way it resembles the global function eval ().
The function () constructor parses the body of the functions 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.
Creating a function using the function () constructor does not follow a typical scope, it always treats it as a top-level function to execute.
Copy CodeThe code is as follows:
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. More details can be found in the functions chapter of the Javascript:the Definitive Guide, 5th Edition.
third, the code pattern of anonymous function Yesterday Hedger Wang introduced several code patterns for anonymous functions in his blog:
Error mode: It does not work, the browser will report syntax error.
Copy CodeThe code is as follows:
function () {
Alert (1);
}();
function literal: Declare a Function object first, and then execute it.
Copy CodeThe code is as follows:
(function () {
Alert (1);
} ) ( );
Precedence expression: Because the JavaScript execution expression is from inside the parentheses to the outside, you can enforce the declared function with parentheses.
Copy CodeThe code is as follows:
(function () {
Alert (2);
} ( ) );
void operator: Use the void operator to execute a single operand that is not enclosed in parentheses.
Copy CodeThe code is as follows:
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
One of the first words in JavaScript's modular 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.
The cornerstone of JavaScript's functional programming (functional programming). See "Writing beautiful JavaScript with functional programming techniques" and "Functional JavaScript Programming Guide" for details.
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.