Home  >  Article  >  Web Front-end  >  Introduction to the differences between JS special functions (Function() constructor, function literal)_Basic knowledge

Introduction to the differences between JS special functions (Function() constructor, function literal)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:33:351145browse
Function definition
A function is declared in this way: keyword function, function name, a set of parameters, and the code to be executed in parentheses.
There are three types of construction syntax for functions:
Js code
Copy 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 quantity

Example:
Js Code
Copy 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 literal

If the function has no clear return value, or calls a return statement without parameters, then the value it actually returns is undefined.

Function() constructor
Function is actually a fully functional object. The Function class can represent any function defined by the developer. The syntax for creating a function directly 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 one The argument is the body of the function (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 is just a variable pointing to the function, so can the function be passed as a parameter to another function? The answer is yes, please see:
Js code
Copy code The code is as follows:

function callAnotherFunc(fnFunction, vArgument) {
fnFunction(vArgument);
}
var doAdd = new Function("iNum", "alert(iNum 10)");
callAnotherFunc(doAdd, 10); //Output "20"

Note: Although you can use the Function constructor to create functions, it is best not to use it because defining functions with it is much slower than using the traditional way. However, all functions should be considered instances of the Function class.
If the function you define has no parameters, you can just pass a string (that is, the body of the function) to the constructor.
Note: None of the parameters passed to the constructor Function() specify the name of the function it is creating. Unnamed functions created with the Function() constructor are sometimes called "anonymous functions". The
Function() function allows us to dynamically build and compile a function without limiting us to the precompiled function body of the function statement.
Function literal
A function literal is an expression that can define an anonymous function. The syntax of a function literal is very similar to that of a function statement, except that it is used as an expression rather than a statement, and there is no need to specify a function name. Syntax:
Js code
Copy code The code is as follows:

var func = function( arg0, arg1, ... argN) { statements };//Function literal

Although function literal creates an unnamed function, its syntax also stipulates that it can specify a function name. This is very useful when writing recursive functions that call themselves, for example:
Js code
Copy 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(), it just allows the function body to refer to itself by this name. Versions of JavaScript prior to 1.5 did not implement this named function literal correctly.
Function reference
The function name has no real meaning. It is just the name of the variable used to save the function. You can assign this function to other variables and it will still start in the same way. Function:
Js code
Copy 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 a function pointer in C.
The difference between Function() constructor and function literal
The difference between Function() constructor and function literal is that the function created using constructor Function() does not use Lexical scope, on the contrary, they are always compiled by top-level functions, such as:
Js code
Copy code code As follows:

var y = "global";
function constructFunction() {
var y = "local";
//Function() constructor
return new Function("return y;");//Do not use local scope
}
function constFunction() {
var y = "local";
//Function literal
var f = function () {
return y;//Use local scope
};
return f;
}
//Display global because the Function() constructor returns The function does not use the local scope
alert(constructFunction()());
//Display global, because the function returns the function directly and uses the local scope
alert(constFunction()()) ;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn