ArticleDirectory
- 1. What is an anonymous function?
- Ii. Differences between function literal and function () constructor
- Iii. Code mode of anonymous Functions
- Iv. Application of anonymous Functions
Original article: javascript anonymous functions 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 JavaScript during runtimeCodeDynamic Creation and compilation. 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.