Differences between special JS functions (Function () constructor and Function direct quantity)

Source: Internet
Author: User

Function Definition

A function is declared in this way: the keyword function, function name, a set of parameters, and the code to be executed in parentheses.

There are three types of function constructor Syntax:

Js Code

The Code is as follows:

1. function functionName (arg0, arg1,... argN) {statements} // function statement

2. var function_name = new Function (arg1, arg2,..., argN, function_body); // Function () constructor

3. var func = function (arg0, arg1,... argN) {statements}; // number of functions directly

Example:

Js Code

The Code is as follows:

1. function f (x) {return x * x}; // function statement

2. var f = new Function ("x", "return x * x;"); // Function () constructor

3. var f = function (x) {return x * x;}; // direct function quantity

If a function does not return a specific value or a return statement without parameters is called, the return value is undefined.

Function () constructor

A function is actually a fully functional object. The Function class can represent any Function defined by the developer. The syntax for directly creating a Function using the Function class is as follows:

Var function_name = new function (arg1, arg2,..., argN, function_body)

In the above form, each arg is a parameter, and the last parameter is the function body (the code to be executed ). These parameters must be strings.

Var sayHi = new Function ("sName", "sMessage", "alert ('hello' + sName + sMessage );");

SayHi ("jzj,", "Hello! "); // Hello jzj, Hello!

The function name only points to the variable of the function. Can a function be passed as a parameter to another function? The answer is yes. See:

Js Code

Copy the Code as follows:

Function callAnotherFunc (fnFunction, vArgument ){

FnFunction (vArgument );

}

Var doAdd = new Function ("iNum", "alert (iNum + 10 )");

CallAnotherFunc (doAdd, 10); // output "20"

Note: Although Function constructor can be used to create a Function, it is better not to use it, because defining a Function with it is much slower than using the traditional method. However, all functions should be considered as Function class instances.

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: 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 called "anonymous functions ".

Function () allows us to dynamically create and compile a function, which does not limit us to the Function body pre-compiled by the function statement.

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. Syntax:

Js Code

The Code is as follows:

Var func = function (arg0, arg1,... argN) {statements}; // number of functions directly

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:

Js Code

The Code is as follows:

Var f = function fact (x ){

If (x <= 1 ){

Return 1;

} Else {

Return x * fact (x-1 );

}

};

Note: it does not actually create a function named fact (), but allows the function body to reference itself with this name. In versions earlier than JavaScript1.5, this type of function is not directly implemented.

• Function reference

The function name does not have any substantive significance. It is just a variable name used to save the function. You can assign this function to other variables and it will still work in the same way:

Js Code

The Code is as follows:

Function square (x) {return x * x ;}

Var a = square;

Var B = a (5); // B is 25

This is a bit like the function pointer in C ++.

Difference between Function () constructor and Function direct quantity

The difference between a Function () constructor and a Function's direct quantity is that functions created using a constructor () do not use lexical scopes. On the contrary, they are always compiled by top-level functions, for example:

Js Code

The Code is as follows:

Var y = "global ";

Function constructFunction (){

Var y = "local ";

// Function () constructor

Return new Function ("return y;"); // The local scope is not used.

}

Function constFunction (){

Var y = "local ";

// Function quantity

Var f = function (){

Return y; // use a local scope

};

Return f;

}

// Display global, because the Function returned by the Function () constructor does not use a local scope

Alert (constructFunction ()());

// Display lobal, because the function is directly returned and uses a local scope

Alert (constFunction ()());

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.