Definition of JavaScript function
1: Call the keyword function to construct, such as:
Function Distance (X1, X2, Y1, Y2)
{{
var dx = x2-x1;
var dy = y2-y1;
Return math.sqrt (dx*dx+dy*dy);
}
2: Use funion () constructing function
var f = new function*"x", "y", "return x*y");
This line of code creates a new function, which is basically equivalent to the functional definition of the syntax definition you are familiar with:
Function f (x, y)
{{
Return x*y;
}
Functino () constructor can accept any multiple string parameters. The main body of the function during its last parameter, which can contain any JavaScript statement, separate the number between statements. Other parameters are used to explain the string of the form parameter name defined by the function. If you define the function without parameters, you can only pass a string (that is, the subject of the function) to the constructor.
Note that none of the parameters passed to the constructor function () are used to explain the function name it to create. Usen -unnamed function created by Function () constructor is sometimes become an "anonymous function".
You may want to know what the function of the function () constructor is used. Why not only use the Function statement to define all functions? The reason is that the Function () constructor allows us to dynamically build and compile a function, which will not limit us to the function body pre -compiled by the function statement. The negative impact effect brought by this is that every time a function is called, the function () constructor must compile it. Therefore, we should not frequently call this constructor in a loop body or in the functions that are often used.
Another reason using the function () constructing function is that it can define the function as part of the expression of JavaScript, rather than defining a statement. In this case, it seems to be more exquisite.
3: Function direct quantity
Function direct quantity is an expression that defines anonymous function. The syntax of the function direct quantity is very similar to the Function statement, but it is used as an expression, not a statement, and it does not need to specify the function name. The following three -line code uses the Function () statement, the function () constructor, and the function of the function to define three basically the same functions:
function f (x) {return x*x};
var f = new function ("x", "return x*x;");
var f = function (x) {reurn x*x};
Although the function directly creates an unnamed function, its syntax also stipulates that it can specify the function name, which is very useful when writing and calling its own recursive function. For example:
var F = Function FACT (X) {if (x <= 1) Return 1; Else Return x*FACT (X-1);};
The above code defines an unnamed function and is stored in variable F for its reference. It does not really create a function called FACT (), but it allows the function to use the name to quote itself. However, it should be noted that the previous version of JavaScript1.5 did not correctly implement the functional function of this name.
The usage of the function of the function is very similar to the method of creating a function with the function () constructing function. Since they are all created by the expression of JavaScript, not created by statements, the way to use them is more flexible, especially for functions that are only used once and do not need to be named. For example, a function specified by a function direct measuring expression can be stored in a variable, passed to other functions, and even directly calls:
a [0] = function (x) {return x*x;}; // Define a function and save it
a.SORT (function (a, b) {return ab;}); // Define one function; pass it to another function
var tensquared = (function (x) {return x*x;}); 10); 10);
Like the function () constructor, the function directly creates an unnamed function and will not automatically store this function into the attribute. However, compared to the Function () constructor, the function direct amount has an important advantage. The subject of a function created by Function () constructing function must be explained by a string, and this way to express a long and complex function is clumsy. But the main body of the function is used by the standard JavaScript syntax. And the function direct amount is resolved once, and the JavaScript code passed to the function () constructing function as a string to the function () constructor is only parsed and compiled once every time the constructor is called.
In JavaScript1.1, the constructor function () can be used to define functions. In JavaScript1.2 and subsequent versions, the function can be used directly to construct the function. You should pay attention to the important differences between these two methods.
First, the constructor function () allows dynamic creation and compilation JavaScript code during runtime. But the function direct quantity is a static part of the function structure, just like the Function statement.
Secondly, as the inevitable result of the first difference, each time the constructor function () is used to analyze the function body and create a New East Han number object. If the call of the constructor appears in a cycle or appears in a function that is often called, the efficiency of this method is very low. On the other hand, the nested function that can directly appear in the cycle and functions is not re -compiled at each call, and a new function object is not created whenever encounters a function direct amount.
The third point between the amount between the function () constructor and the function is that the function created by the constructor Function () does not use the phrase scope. On the contrary, they are always compiled as a top function, just like The following code is explained:
var y y = "global";
Function ConstructionFunction () ()
{{
var y y = "local";
Return New Function ("Return Y"); // Do not capture the local scope.
}
// This line of code will display "Global", because the function returned by the function () constructor does not use the local scope.
// If you use a function direct amount, this line of code may display "Local".
alert (constructure ());