Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of Eval function in JavaScript and how to execute code string

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people are at a loss about the introduction of the Eval function in JavaScript and how to execute the code string. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Eval: execute code string

The built-in function eval allows you to execute a code string.

The syntax is as follows:

Let result = eval (code)

For example:

Let code = 'alert ("Hello")'; eval (code); / / Hello

The code string may be long, including newline characters, function declarations, variables, and so on.

The result of eval is the result of the last statement.

For example:

Let value = eval ('1cm 1')

Alert (value); / / 2

Let value = eval ('let I = 0; + + i'); alert (value); / / 1

The code within eval executes in the current lexical environment (lexical environment), so it can access external variables:

Let a = 1; function f () {let a = 2; eval ('alert (a)'); / / 2} f ()

It can also change external variables:

Let x = 5; eval ("x = 10"); alert (x); / / 10, the value has been changed

In strict mode, eval has its own lexical environment. Therefore, we cannot externally access functions and variables declared in eval:

/ hint: all runnable examples in this tutorial have strict mode 'use strict' eval ("let x = 5; function f () {}") enabled by default; alert (typeof x); / / undefined (without this variable) / / function f is also inaccessible from the outside

If strict mode is not enabled, eval does not have its own lexical environment, so we can access variables x and function f from outside.

Use "eval"

Eval is seldom used in modern programming. People often say that "eval is the devil".

The reason is simple: a long time ago, JavaScript was a very weak language, and a lot of things could only be done through eval. But that was ten years ago.

There is little reason to use eval these days. If someone is using it, this is a good opportunity to replace them with modern language structures or JavaScript Module [1].

Note that eval's ability to access external variables has side effects.

The code compression tool (the tool that compresses JS before it is put into production) renames local variables to shorter variables such as an and b to make the code smaller. This is usually safe, but not when eval is used, because local variables may be accessed by code in eval. Therefore, the compression tool does not rename all variables that may be accessed from eval. This results in a lower code compression ratio.

Using external local variables in eval is also considered a bad programming habit because it makes code maintenance more difficult.

There are two ways to avoid such problems altogether.

If the code in eval does not use external variables, use window.eval (...) Call eval in the form of:

In this way, the code executes in the global scope:

Let x = 1; {let x = 5; window.eval ('alert (x)'); / / 1 (global variable)}

If the code in eval needs to access local variables, we can use new Function instead of eval and pass them as parameters:

Let f = new Function ('averse,' alert (a)'); f (5); / / 5

The new Function constructor is described in detail in the chapter [new Function "syntax] (https://zh.javascript.info/new-function" new Function "syntax). New Function creates a function from a string and is also in global scope. So it cannot access local variables. However, as in the example above, it is much clearer to explicitly pass them as parameters.

Summary

Calling eval (code) runs the code string and returns the result of the last statement.

In modern JavaScript programming, it is rarely used, and usually not needed.

You can access external local variables. This is considered to be a bad programming habit.

To eval the code in the global scope, you can use window.eval (code) instead.

In addition, if your code needs to get data from an external scope, use new Function and pass the data as arguments to the function.

Homework problem

Make your own questions before you look at the answers.

Eval- calculator

Degree of importance:?

Create a calculator that prompts the user for an arithmetic expression and returns the result of its evaluation.

In this question, you don't need to check whether the expression is correct. You just need to calculate and return the results.

After reading the above, have you mastered the introduction of the Eval function in JavaScript and how to execute the code string? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report