The difference between JS special function (function () constructor, function direct amount)

Source: Internet
Author: User

function Definition
Functions are declared in such a way that the keyword function, the function name, a set of parameters, and the pending code that is placed in parentheses.
There are three kinds of constructor syntax for a function:

1.function functionname (arg0, arg1, ... argN) {statements}//2. var New Function (Arg1, arg2, ..., ArgN, function_body); //  3.varfunction(arg0, arg1, ... argN) {statements}; //

Example:

function f (x) {return x*x}; //  varnew Function ("x", "Return x*x;"); //  varfunction(x) {return x*x;}; //

If the function has no explicit return value, or if a return statement with no arguments is called , then the value it really returns is undefined.

function () constructor

The function is actually a fully functional object. The function class can represent any functions defined by the developer. The syntax for creating functions directly with 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 argument is the function body (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, so can you pass the function as an argument to another function? The answer is yes, please see:

functionvarnew function ("INum", "alert (INum +)"//

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 the traditional way. However, all functions should be considered as instances of the function class.
If you define a function that has no parameters, you can simply pass a string (that is, the body of the function) to the constructor.
Note: None of the arguments passed to the constructor function () is used to describe the function name that it is creating. Unnamed functions created with the function () constructor are sometimes referred to as "anonymous functions."
The function () functions allow us to dynamically build and compile a function that does not limit us to the body of a function statement that is precompiled.

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

vary = "global"; functionconstructfunction () {vary = "local"; //function () constructorreturn NewFunction ("Return y;");//do not use local scopes} functionconstfunction () {vary = "local"; //Function Direct Quantityvarf =function () { returnY//using local scopes}; returnF;} //show global because functions returned by the function () constructor do not use local scopesalert (Constructfunction () ());//show Lobal because the function directly returns the function and uses the local scopeAlert (Constfunction () ());

The difference between JS special function (function () constructor, function direct amount)

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.