JavaScript中eval的预期目的是什么? [英] What is the intended purpose of eval in JavaScript?

查看:52
本文介绍了JavaScript中eval的预期目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JavaScript的eval()什么时候不邪恶?

我知道,通常使用 eval()是不好的做法.

I know, generally using eval() is bad practice.

但是它在那里存在的目的是什么?

But for what purpose it is existed there?

eval()的正确目的是什么?

在什么情况下最好使用 eval()?

At what situation it is better to use eval()?

推荐答案

评估

eval

计算一串JavaScript代码无需提及特定内容对象.

Evaluates a string of JavaScript code without reference to a particular object.

如果将算术表达式构造为字符串,则可以在以后使用eval对其求值.例如,假设您有一个变量x.您可以通过将表达式的字符串值(例如"3 * x + 2")分配给变量,然后在脚本的后面调用eval来推迟对涉及x的表达式的求值.

If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.

eval()是一个危险的函数,该函数以调用者的特权执行它传递的代码.如果您使用可能受到恶意方影响的字符串运行eval(),则最终可能会在您的网页/扩展程序许可的情况下在用户计算机上运行恶意代码.

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension.

这篇关于JavaScript中eval的预期目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆