JS Special function (function () constructor, function direct quantity) difference

Source: Internet
Author: User
Tags anonymous constructor function definition

function definition

A function is declared in such a way that it is a keyword function, a function name, a set of parameters, and the pending code placed in parentheses.

The constructor syntax for a function has these three types:

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};//function Direct amount

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;};/ /function Direct Quantity

If the function does not have an explicit return value, or if it calls a returned statement with no arguments, then the value it really returns is undefined.

function () constructor

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

var function_name = new Function (arg1, arg2, ..., argn, function_body)

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

var sayhi = new Function ("Sname", "smessage", "Alert" (' Hello ' + sname + smessage); ");

Sayhi ("Jzj,", "Hello!"); /hello jzj, Hello!

The function name is just a variable that points to a function, can you pass the function as an argument to another function? The answer is yes, please see:

JS Code

Copy code code as follows:

function Callanotherfunc (fnfunction, vargument) {

Fnfunction (vargument);

}

var doadd = new Function ("Inum", "alert (Inum + 10)");

Callanotherfunc (Doadd, 10); Output "20"

Note: Although functions can be created using the function constructor, it is best not to use it because it is much slower to define a function than in traditional ways. However, all functions should be treated as instances of the function class.

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: 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 referred to as an "anonymous function."

function () functions allow us to dynamically build and compile a function that does not limit us to functions that are precompiled in function statements.

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

JS Code

The code is as follows:

var func = function (arg0, arg1, ... argn) {statements};//function Direct amount

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, such as:

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 really create a function named fact (), but only allows the function body to refer to itself with that name. The direct amount of this named function was not properly implemented in the previous version of JavaScript1.5.

• Function Reference

The function name has no real meaning, it is just used to hold the variable name of the function, you can assign this function to other variables, 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

It's kind of like a function pointer in C + +.

function () constructor and direct amount difference

The difference between the function () constructor and the direct amount of the function is that functions created using constructor function () do not use lexical scopes, instead they are always compiled by top-level functions, such as:

JS Code

The code is as follows:

var y = "global";

function Constructfunction () {

var y = "local";

function () constructor

Return the new Function ("Return y;"); /Do not use local scope

}

function Constfunction () {

var y = "local";

Function Direct Quantity

var f = function () {

Return y;//use local scope

};

return F;

}

Show global because functions returned by function () constructors do not use local scopes

Alert (Constructfunction () ());

Show Lobal, because functions returned by direct volume and use local scopes

Alert (Constfunction () ());

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.