Let's take a look at this example:
Function (){
Alert ("");
Return function B () {// return function () {alert ('B ');}
Alert ('B ');
}
}
This is a function that returns a function. So what is the result of the first execution of this code? What is the result of another execution?
First, let's analyze this code:
1. This code is a function with the name a and no parameters;
2. There is also a function in this function. The function name is B. There is also a return statement in function B, which is a function.
In this case, we need to understand the concept that a function is a data type. This concept is very important for us to understand this code.
Since a function is a data type, a function can be defined, deleted, copied, or even transmitted as a parameter of another function, just like a variable.
If you just put the above code function a in a piece of code that is being executed, the code will not run. Why?
This is because: this code is not called, that is, the statement of function a. It just defines what function a is.
To execute the code in function a, you must add the following statement: a (); call function a; if the function has a parameter, it is the value to be passed to the parameter.
The first execution result of the above code is:
1. alert ("A"); A pop-up box with the content A is displayed.
2. Return function B to function.
Note: Although the code is executed, we cannot see the obvious effect. It can be seen only when the function is executed again and the alert statement in function B is executed, you can think of function B as a common variable, but return it to function.
Next, we want the function to be executed again. You can perform the following operations:
Var newf = a (); // returns function B to function a, but function B is not called at this time.
Nexf (); // call function B and execute the statements in function B.
The result is as follows:
Alert ('B'); a pop-up box is displayed, with the content B.
In fact, if you want the returned function to be executed immediately, you do not need to assign it to the variable. Simply add a pair of parentheses after the call. The effect is the same.
A ()();
Supplement:
1. A function is a data type.
Functions in javascript are data. This special data type has two important features:
@ They contain code
@ They are executable (or callable)
2. Functions can be defined, deleted, copied, or even transmitted as parameters of another function, just like variables.
3. The function name cannot start with a number. It can be composed of any letter, number, or underline.
This article from the "Ordinary Girl's Trip" blog, please be sure to keep this source http://jones.blog.51cto.com/9565009/1574008
Return function