


Detailed explanation of the definition, usage and side effects of JavaScript global function eval with examples
eval() is a global function, javascript uses eval() to interpret and run a string composed of javascript source code
var result = eval('3+2'); console.log(result,typeof result);//5 'number'
Usage
eval() has only one parameter. If the parameter passed in is not a string, it returns this parameter directly. If the parameter is a string, it will compile the string as javascript code. If compilation fails, a syntaxError exception is thrown. If the compilation is successful, execution of this code begins and the value of the last expression or statement in the string is returned. If the last expression or statement has no value, undefined is finally returned. If string throws an exception, this exception will pass the call to eval()
var num = 1; var str = 'test'; console.log(eval(num));//1 console.log(eval(str));//ReferenceError: test is not defined var strLong1 = 'var x = 1;var y = 2;'; console.log(eval(strLong1),x,y);//undefined 1 2 var strLong2 = 'var x = 1; x++;'; console.log(eval(strLong2),x);//1 2
scope
eval() uses the variable scope environment in which it is called. That is, it looks up the values of variables and defines new variables and functions exactly the same as code in the local scope
var b = 2; function foo(str,a){ eval(str); console.log(a,b); } foo('var b = 3;',1);//1 3
alias
When called through an alias, eval() will execute its string as top-level global code. The executed code may define new global variables and global functions, or assign values to global variables, but it cannot use or modify local variables in the function
var geval = eval; var x = 'global',y = 'global'; function f(){ var x = 'local'; eval('x += "changed";'); return x; } function g(){ var y = 'local'; geval('y += "changed";'); return y; } console.log(f(),x);//localchanged global console.log(g(),y);//local globalchanged
[Note] IE8-The result of calling eval() through an alias in the browser is the same as calling eval() normally
Side effects
The javascript interpreter has done a lot Code analysis and optimization. The problem with eval() is that the code used for dynamic execution usually cannot be analyzed, so the interpreter cannot optimize it, which will lead to performance degradation
Similar to eval() is setTimeout (), setInterval(), new Function(), etc. These functions can take strings as parameters and be executed dynamically when the program is running. The benefits of this execution mechanism cannot offset its performance loss, so you should try to avoid using
strict mode
Due to eval( ) function is too powerful, strict mode imposes strict restrictions on it
[1]Variables or functions cannot be created through the eval() function, but their values can be queried and changed
'use strict'; eval('var x = 1;'); console.log(x);//ReferenceError: x is not defined 'use strict'; var x = 1; eval('x = 2;'); console.log(x);//2
【2】It is forbidden to use eval as an identifier
'use strict'; var eval = 10;//SyntaxError: Unexpected eval or arguments in strict mode
The above is the detailed content of Detailed explanation of the definition, usage and side effects of JavaScript global function eval with examples. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
