Explanation of anonymous Function Analysis in javascript

Source: Internet
Author: User
There are three methods to define a function in Javascript: function statement: functionfnMethodName (x) {alert (x);} function literal (FunctionLiterals): varfnMethodNamefunction (x) {alert (x);} Function () construction...

There are three methods 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 );')


An anonymous function is a function without a name. For example:

 
Function () {alert ('a function ');}
 

However, the above Code will report an error. Firebug prompt: function statement requires a name, that is, the function must have a name.


The strange thing is that if I use a pair of () to package the function with no name, no error will be reported. For example:

 
(Function () {alert ('a function ');})
 

(Note the () of the function package ()!). Although no error will be reported in this way, who knows if this function is declared successful? Is it because there is no declaration? Let's test: Let the function execute it once:

 
(Function () {alert ('a function ');}())
 

As you can see, the function is executed, indicating that the function exists.

Similarly, if you remove the wrapped function () at this time, the previous error will still be reported and the function cannot be executed...

 
Function () {alert ('a function ');}()
 


Anonymous Functions

Let's take a look at the Google Code. JavaScript actually supports the definition of the number of rows in this form:

 
Function (msg) {alert (msg);} ("http://www.hzhuti.com/nokia/5236 ");
 

This is actually two steps: the first step is to define a function,

Equivalent:

 
Var abc = function (msg) {alert (msg );}

Step 2: Execute it immediately:

 
Abc ("hello world ");

Concatenate the two statements and remove the name of the abc function.

 

Encapsulation

Another benefit may be better encapsulation. For example, there are many function definitions in this function, such

 
Q (); p (); m (); g (); I ()
 

There are also a large number of variables:

 
Var j; var h; var l;

And so on. In JavaScript, there is no simple private method to define private functions or private functions. If the caller can access these intermediate members at Will (which may change or even be removed at any time. It is indeed a terrible thing for such an open-minded and unobstructed object, from the perspective of an API provider (and the user's perspective.

If you put it in an anonymous function, there is no way to directly access the function in it. The internal logic is perfectly encapsulated. In this way, how secure these functions and variables are!

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 ()

Constructors allow 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.

 
Var y = "global ";
Function constructFunction (){
Var y = "local ";
Return new Function ("return y"); // The local variable cannot be obtained.
}
Alert (constructFunction (); // output "glob

 

From php development

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.