Summary of several differences in javascript function definitions

  • 2020-03-30 01:13:00
  • OfStack

Javascript function definition

1: call the keyword function to construct, such as:
The function short (x1, x2, y1, y2)
Var dx = x2 - x1;
Var dy = y2 - y1;
Return math.h SQRT (dy dx + dy dx * *);

2: use the Function() constructor
Var f = new Function * "x", "y", "return x * y");
This line of code creates a new function that is basically equivalent to a function defined in your familiar syntax:

The function f (x, y)
Return x * y;

The Functino() constructor can take any number of string arguments. Its last argument is the body of the function, which can contain any JavaScript statements separated by semicolons. The other arguments are strings that describe the formal argument name that the function is defining. If you define a function that has no arguments, you can simply pass a string (the body of the function) to the constructor.

Notice that none of the arguments passed to the constructor Function() specify the name of the Function it is to create. Unnamed functions created with the Function() constructor are sometimes called "anonymous functions".

You may be curious to know what the Function() constructor is used for. Why not define all functions using only the function statement? The reason is that the Function() constructor allows us to build and compile a Function dynamically, and it does not limit us to the body of the Function precompiled by the Function statement. The downside of this is that the Function() constructor compiles a Function every time it is called. Therefore, we should not call this constructor frequently in the body of the loop or in functions that are commonly used.

Another reason to use the Function() constructor is that it is able to define a Function as part of a JavaScript expression, rather than as a statement, in which case it is relatively, even delicately, used.

3: function direct quantity
A function direct is an expression that defines an anonymous function. The syntax for function direct quantities is very similar to the function statement, except that it is used as an expression, not a statement, and there is no need to specify a function name. The following three lines of code define three basically identical functions using the function() statement, the Funciont() constructor, and the function direct quantity, respectively:

The function f (x) {return * x x};
Var f = new Function (" x ", "return * x x;" );
{reurn var = function f (x) x x x};

Although a function directly creates an unnamed function, its syntax also dictates that it can specify the function name, which is useful when writing recursive functions that call itself. Such as:
Var f = function fact (x) {if (x< = 1) return 1; The else return x * fact (x - 1); };
The above code defines an unnamed function and the reference to it is stored in the variable f. It doesn't actually create a function called fact(), just allows the body of the function to refer to itself by that name. Note, however, that previous versions of JavaScript1.5 did not correctly implement this named function direct.

The use of Function direct quantities is very similar to the way a Function is created with the Function() constructor. Because they are created by JavaScript expressions rather than statements, they are more flexible to use, especially for functions that are used only once and don't need to be named. For example, a function specified using a function direct expression can be stored in a variable, passed to another function, or even called directly:

A [0] = function (x) {return * x x; }; // define a function and save it
A.s ort (function (a, b) {return a - b; }); // define a function; Pass it to another function
Var tensquared = (function (x) {return * x x; }) (10);

Like the Function() constructor, the Function directly creates an unnamed Function and does not automatically store the Function in a property. However, the Function direct has an important advantage over the Function() constructor. The body of a Function created by the Function() constructor must be described in a string, which is a clumsy way to express a long and complex Function. But the body of the function's direct volume USES standard JavaScript syntax. While the Function direct is parsed only once, the JavaScript code passed as a string to the Function() constructor is parsed and compiled only once each time the constructor is called.

In JavaScript1.1, you can use the constructor Function() to define a Function, and in JavaScript1.2 and later, you can use the Function direct to construct a Function. You should note the important difference between the two methods.

First, the constructor Function() allows you to dynamically create and compile JavaScript code at run time. But the function direct is a static part of the function structure, just like the function statement.

Second, as a corollary of the first distinction, each time the constructor Function() is called, the body of the Function is parsed and a new donghan object is created. This method is very inefficient if the call to the constructor occurs in a loop or in a function that is frequently called. On the other hand, function direct quantities or nested functions that appear in loops and functions are not recompiled every time they are called, and no new function object is created each time a function direct quantity is encountered.

The third difference between the quantity of the Function() constructor and the quantity of the Function is that functions created using the constructor Function() do not use lexical scope; instead, they are always compiled as top-level functions, as shown in the following code:

Var y = "global";
The function constructFunction ()
Var y = "local";
The return of new Function (return "y"); // local scope is not captured.
// this line of code displays "global" because the Function() constructor returns a Function that does not use local scope.
// this line of code might show "local" if a function direct quantity is used.
Alert (constructFunction ());

Related articles: