Comparison of common JavaScript-defined functions in _javascript techniques

Source: Internet
Author: User

More common JavaScript definition of the difference between functions mainly through the following three aspects to explain to you, need a friend to refer to the next bar

1: Call the keyword function to construct the

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:

 
 

This line of code creates a new function that is essentially equivalent to the function you are familiar with in the syntax definition:

function f (x,y) 
{return  x*y;   

The Functino () constructor can accept any number of string parameters. Its last argument is the body of the function, which can contain any JavaScript statements, separated by semicolons. Other parameters are strings used to describe the formal parameter names that the function is to define. If you define a function that has no parameters, you can simply pass a string (i.e. the body of the function) to the constructor.

Note that none of the parameters passed to the constructor function () is used to describe the name of the function that it is creating. An unnamed function created with a function () constructor is sometimes called an anonymous function.

You may be very interested to know what the function () constructor is used for. Why not use Function statements to define all the functions? The reason is that the function () constructor allows us to dynamically build and compile functions that do not limit us to functions that are precompiled in function statements. The downside effect of doing this is that the function () constructor compiles every time you call one of the functions. Therefore, we should not call this constructor frequently in a loop body or in a function that is often used.

Another reason to use the function () constructor is that it can define a function as part of a JavaScript expression rather than define it as a statement, in which case it is even more elegant to use it.

3: Function Direct Quantity

The direct amount of a function is an expression that can define an anonymous function. The syntax of the direct volume of a function is very similar to a function statement, except that it is used as an expression, not as a statement, and does not need to specify a function name. The following three lines of code define three essentially identical functions using the function () statement, the Funciont () constructor, and the direct amount of the function:

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

Although the direct amount of a function creates an unnamed function, its syntax also specifies that it can specify a function name, which is useful when writing recursive functions that call itself.

For example:

 
 

The code above defines an unnamed function, and its reference is stored in the variable F. It doesn't really create a function called fact (), it just allows the function body to use that name to refer to itself. Note, however, that the direct amount of this named function is not properly implemented in the previous version of JavaScript1.5.

The use of the direct volume of a function is very similar to the method used to create a function with its function () constructor. Since they are all created by JavaScript expressions rather than by statements, the way they are used is more flexible, especially for functions that are used only once and do not need to be named. For example, a function specified using a function direct quantity 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.sort (function (a,b) {return a-b;}); /define a function; pass it to another function 

As with the function () constructor, the direct amount of functions creates an unnamed function and does not automatically store the function in the property. However, there is an important advantage to the direct quantity of a function, compared to the function () constructor. The body of a function created by the function () constructor must be described with a string, and it is clumsy to express a long and complex function in this way. But the body of the direct volume of the function uses the standard JavaScript syntax. Moreover, the direct amount of the function is parsed only once, while the JavaScript code passed as a string to the function () constructor is only parsed once and compiled once each time the constructor is invoked.

In JavaScript1.1, you can use the constructor function function () to define functions, and in JavaScript1.2 and subsequent versions, you can also use the direct amount of the function to construct the function. You should pay attention to the important difference between the two methods.

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

Second, as the inevitable result of the first difference, the function body is parsed each time the constructor function function () is invoked and a new East Han number object is created. This method is very inefficient if a call to a constructor appears in a loop, or in a function that is often called. In another respect, the function's direct volume or nested functions appearing in loops and functions are not recompiled at every call, and a new function object is not created whenever a direct amount of the function is encountered.

The 3rd difference between the number of function () constructors and functions is that functions created using constructor function () do not use lexical scopes, instead they are always compiled as top-level functions, as explained in the following code:

var y= "global"; 
function constructfunction () 
{ 
var y= "local"; 
return new function (' Return y ');//Do not capture local scope.   
} 
This line of code will display "global" because functions returned by function () constructors do not use local scopes. 
//If you use a direct amount of a function, this line of code may display "local". 

Related Article

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.