Anonymous functions can effectively ensure that Javascript is written on the page without causing global variables to be contaminated.
This works very well and is beautiful when adding Javascript to a page that is not very familiar.
1. What is an anonymous function?
There are generally three ways to define a function in Javascript:
Function keyword 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 first is the most commonly used method. The latter two are to copy a function to the variable fnMethodName, and this function has no name, that is, anonymous function.
In fact, quite a lot of languages have anonymous functions.
2. The difference between function literals and Function() constructors
Although the function literal is an anonymous function, the syntax allows it to be specified with any function name. It can be called itself when writing a recursive function, but it cannot be done using the Function() constructor.
var f = function fact(x) { if (x < = 1) return 1; else return x*fact(x-1); };
The Function() constructor allows dynamic creation and compilation of Javascript code at runtime. In this way it is similar to the global function eval().
The Function() constructor parses the function body every time it is executed and creates a new function object. Therefore, the efficiency of calling the Function() constructor in a loop or frequently executed function is very low. On the contrary, function literals are not recompiled every time they encounter it.
When creating a function with the Function() constructor, it does not follow the typical scope, it always executes it as a top-level function.
var y = "global";
function constructFunction() { var y = "local"; return new Function("return y"); // Cannot get local variable} alert(constructFunction()()); // Output "global" and function keyword definition have their own characteristics and are much more difficult to use than Function() constructors.
So this technology is usually rarely used.
The function literal expression and function keyword definition are very similar.
Considering the previous difference, although there are messages saying that literal anonymous functions have bugs under certain webkit engines under OS X 10.4.3,
But what we usually call anonymous functions refer to anonymous functions that use the literal form of function.
3. Code mode of anonymous functions
Error mode: It cannot work, the browser will report a syntax error.
function(){ alert(1); }();
Function literals: First declare a function object, and then execute it.
(function(){ alert(1); } ) ( );
Preferential expression:
( function(){ alert(2); } ( ) );
void operator:
void function(){ alert(3); }() are the same. Hedger wang prefers the third type for personal reasons, while in actual applications, I see and use the first type.
4. Application of anonymous functions
The first sentence in "A Module Pattern of Javascript" is "Global variables are the devil."
With the var keyword, anonymous functions can effectively ensure that Javascript is written on the page without causing pollution to global variables.
This works very well and is beautiful 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 other Javascript libraries also use a lot of them.
The cornerstone of functional programming in Javascript.
For details, please see "Writing Beautiful JavaScript with Functional Programming Technology" and "Guide to Functional JavaScript Programming".
The above detailed explanation of the usage and advantages and disadvantages of anonymous functions in JavaScript is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.