


Detailed examples of the difference between javascript function literals and Function() constructors
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!

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
