Home  >  Article  >  Web Front-end  >  Detailed examples of the difference between javascript function literals and Function() constructors

Detailed examples of the difference between javascript function literals and Function() constructors

伊谢尔伦
伊谢尔伦Original
2017-07-27 16:58:522343browse

The difference between function literal and Function() constructor
Although function literal is an anonymous function, the syntax allows you to specify any function name for it. When writing a recursive function, you can call it yourself, using Function( ) constructor does not work.

var f = function fact(x) { 
if (x < = 1) return 1; 
else return x*fact(x-1);
};

The Function() constructor allows Javascript code to be dynamically created and compiled at runtime. In this way it is similar to the global function eval().

The Function() constructor parses the function body and creates a new function object each time it is executed. Therefore, the efficiency of calling the Function() constructor in a loop or frequently executed function is very low. In contrast, function literals are not recompiled every time they are encountered.

When creating a function using the Function() constructor, it does not follow the typical scope. It always executes it as a top-level function.

var y = "global"; 
function constructFunction() { 
var y = "local"; 
return new Function("return y"); // 无法获取局部变量} 
alert(constructFunction()()); // 输出 "global" 函数直接量:

As long as it is an expression syntax, the script host considers function to be a direct function. If nothing is added, just starting with function is considered to be a function declaration, and function is written into an expression. , such as the four arithmetic operations, the host will also treat it as a direct quantity, as follows:

var a = 10 + function(){ 
return 5; 
}();
(function(){ 
alert(1); 
} ) ( ); 
( function(){ 
alert(2); 
} ( ) ); 
void function(){ 
alert(3); 
}() 
0, function(){ 
alert(4); 
}(); 
-function(){ 
alert(5); 
}(); 
+function(){ 
alert(6); 
}(); 
!function(){ 
alert(7); 
}(); 
~function(){ 
alert(8); 
}(); 
typeof function(){ 
alert(9); 
}();

There are many ways to define functions in js, and function direct quantity is one of them. For example, var fun = function(){}, if function is not assigned to fun, then it is an anonymous function.

See how the anonymous function is called.

1. Function calls that get return values ​​after execution

//方式一,调用函数,得到返回值。强制运算符使函数调用执行 
(function(x,y){ 
alert(x+y); 
return x+y; 
}(3,4)); 
//方式二,调用函数,得到返回值。强制函数直接量执行再返回一个引用,引用在去调用执行 
(function(x,y){ 
alert(x+y); 
return x+y; 
})(3,4);

2. Ignore return values ​​after execution

//方式三,调用函数,忽略返回值 
void function(x) { 
x = x-1; 
alert(x); 
}(9);

Well, finally look at the wrong calling method

//错误的调用方式 
function(x,y){ 
alert(x+y); 
return x+y; 
}(3,4);

The above is the detailed content of Detailed examples of the difference between javascript function literals and Function() constructors. For more information, please follow other related articles on the PHP Chinese website!

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