Differences between javascript function definitions _ javascript skills

Source: Internet
Author: User
This article mainly summarizes several differences in the definition of javascript Functions. If you need some help, please refer to them. Javascript function definition

1: Construct a function by calling a keyword, for example:
Function distance (x1, x2, y1, y2)
{
Var dx= x2-x1;
Var dy = y2-y1;
Return Math. sqrt (dx * dx + dy * dy );
}

2: Use Function () to construct a Function
Var f = new Function * "x", "y", "return x * y ");
This line of code creates a new function, which is basically equivalent to the syntax defined function you are familiar:

Function f (x, y)
{
Return x * y;
}

The Functino () constructor can accept any number of string parameters. Its last parameter is the main body of the function, which can contain any JavaScript statements, separated by semicolons. Other parameters are strings that describe the formal parameter name to be defined by the function. If the defined function does not have a parameter, you only need to pass a string (the main body of the function) to the constructor.

Note that none of the parameters passed to the constructor Function () indicates the name of the Function to be created. Untitled functions created by using Function () constructor are sometimes "anonymous functions ".

You may want to know the purpose of the Function () constructor. Why can't we use only function statements to define all functions? The reason is that the Function () constructor allows us to dynamically create and compile a function. It does not limit us to the Function body pre-compiled by the function statement. The negative effect of this operation is that each time a Function is called, the Function () constructor must compile it. Therefore, we should not call this constructor frequently in the loop body or in frequently used functions.

Another reason for using Function () to construct a Function is that it can define a Function as part of a JavaScript expression, rather than defining a statement. In this case, it is quite helpful to use it, it can even be said to be exquisite.

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

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

Although a function is directly created with an unnamed function, its syntax also stipulates that it can specify the function name, which is very useful in 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 code above defines an unnamed function and stores its reference in variable f. It does not actually create a function named fact (), but allows the function body to reference itself with this name. However, in versions earlier than JavaScript1.5, the direct quantity of such functions is not implemented correctly.

The usage of direct Function quantity is very similar to the method used to create a Function using Function. Because they are all created by JavaScript expressions, rather than statements, they are more flexible to use, especially for functions that only use once and do not need to be named. For example, a function specified using a function's direct amount expression can be stored in a variable, passed to other functions, 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;}); // defines a function and passes it to another function.
Var tensquared = (function (x) {return x * x;}) (10 );

Like a Function () constructor, a Function is directly created with an unnamed Function and does not automatically store the Function in attributes. However, compared with Function () constructor, direct Function quantity has an important advantage. The subject of the Function created by the Function () constructor must be described using a string. It is clumsy to express a long and complex Function in this way. However, the main body of the function is directly using the standard JavaScript syntax. In addition, the number of functions is parsed only once, and the JavaScript code passed as a string to the Function () constructor is parsed and compiled once each time the constructor is called.

In JavaScript1.1, you can use the constructor () to define the Function. In JavaScript1.2 and later versions, you can also use the Function to construct the Function directly. Pay attention to the important differences between the two methods.

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

Second, as the inevitable result of the first difference, every call to the constructor Function () will parse the Function body and create a new number object. If a constructor is called in a loop or in a frequently called function, this method is very inefficient. On the other hand, functions directly or nested functions in loops and functions are not re-compiled each time they are called, in addition, no new function object is created when a function is directly called.

The third difference between the number of Function constructor and Function is that the Function created using the constructor Function () does not use the lexical scope. On the contrary, they are always compiled as top-level functions, as described in the following code:

Var y = "global ";
Function constructFunction ()
{
Var y = "local ";
Return new Function ("return y"); // partial scopes are not captured.
}
// This line of code will display "global", because the Function returned by the Function () constructor does not use a local scope.
// If a function is directly used, this line of code may display "local ".
Alert (constructFunction ());

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.