The more common differences between defined functions in javascript

  • 2020-10-23 20:50:54
  • OfStack

The differences of defined functions in javascript are mainly explained in the following three aspects. Please refer to them if you need

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 the Function() constructor

Such as:


var f=new Function*"x","y","return x*y"); 

This line of code creates a new function that is basically equivalent to the syntactically defined function you are familiar with:


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 names that the function is defining. If you define a function that takes no arguments, you can simply pass the constructor a string (that is, the body of the function).

Note that none of the arguments passed to the constructor Function() describe the name of the function it is creating. Unnamed functions created with the Function() constructor are sometimes called "anonymous functions."

You might be curious to know what the purpose of the Function() constructor is. Why can't you define all the functions using only the function statement? The reason is that the Function() constructor allows us to create and compile a function on the fly, and it does not restrict us to the body of the function that the function statement precompacts. 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 a loop or in frequently used functions.

Another reason to use the Function() constructor is that it defines the function as a part of an JavaScript expression rather than a statement, in which case it is relatively, or even delicate, to use.

3: Function direct quantity

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


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

Although the function direct creates an unnamed function, its syntax also dictates that it can specify the function name, which is useful when writing recursive functions that call themselves.

Such as:


var f=function fact(x){if(x<=1)return 1;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 properly implement this named function direct quantity.

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


a[0]=function(x){return x*x;};// define 1 A function and save it  
  a.sort(function(a,b){return a-b;});// define 1 A function; Pass it on to the other 1 A function  
  var tensquared=(function(x){return x*x;})(10); 

Like the Function() constructor 1, the function directly creates an unnamed function and does not automatically store the function in the property. However, functional direct quantities have one important advantage over Function() constructors. The body of a function created by the Function() constructor must be specified in a string, which is an unwieldy way to express a long and complex function. But the body of the function direct USES the standard JavaScript syntax. And the function direct is parsed only once, whereas 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 the function, and in ES73en1.2 and later, you can also use the function direct quantity to construct the function. You should be aware of an important difference between the two approaches.

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

Second, as an inevitable consequence of the first difference, every time the constructor Function() is called, the body of the function is parsed and a new Eastern Han number object is created. This method is very inefficient if the call to the constructor occurs in a loop or in a frequently invoked function. 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 a new function object is not created every time a function direct quantity is encountered.

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


 var y="global"; 
  function constructFunction() 
  { 
    var y="local"; 
    return new Function("return y");// Local scope is not captured.  
  } 
  // This line of code will show "global" Because the Function() The constructor returns a function that does not use local scope.  
  // If use 1 A direct function, this line of code may be displayed "local" .  
  alert(constructFunction()); 


Related articles: