JavaScript 的 eval() 什么时候不是邪恶的?
我正在编写一些 JavaScript 代码来解析用户输入的函数(用于类似电子表格的功能)。 解析完公式后,我可以将其转换为 JavaScript 并对其运行 eval()
以产生结果。
然而,如果可以避免的话,我总是回避使用 eval()
因为它是邪恶的(而且,无论正确与否,我一直认为它在 JavaScript 中更加邪恶,因为要评估的代码可能会被用户更改)。
那么,什么时候可以使用呢?
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval()
on it to yield the result.
However, I've always shied away from using eval()
if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is even more evil in JavaScript, because the code to be evaluated might be changed by the user).
So, when it is OK to use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
我想花点时间来解决您问题的前提 - eval() 是“邪恶”。 编程语言人员使用的“邪恶”一词通常意味着“危险”,或更准确地说“能够通过看似简单的命令造成很多伤害”。 那么,什么时候可以使用危险的东西呢? 当您知道危险是什么并且采取适当的预防措施时。
言归正传,我们来看看使用eval()的危险性。 就像其他事情一样,可能存在许多小隐患,但两大风险 - eval() 被认为是邪恶的原因 - 是性能和代码注入。
根据您的具体情况。 据我了解,您自己生成字符串,因此假设您小心不要生成像“rm -rf Something-important”这样的字符串,则不存在代码注入风险(但请记住,它是 在一般情况下很难确保这一点)。 另外,我相信,如果您在浏览器中运行,那么代码注入的风险相当小。
至于性能,您必须权衡编码的便捷性。 我认为,如果您正在解析公式,您最好在解析期间计算结果,而不是运行另一个解析器(eval() 内的解析器)。 但使用 eval() 编码可能更容易,并且性能影响可能不会明显。 在这种情况下,看起来 eval() 并不比任何其他可能节省您时间的函数更邪恶。
I'd like to take a moment to address the premise of your question - that eval() is "evil". The word "evil", as used by programming language people, usually means "dangerous", or more precisely "able to cause lots of harm with a simple-looking command". So, when is it OK to use something dangerous? When you know what the danger is, and when you're taking the appropriate precautions.
To the point, let's look at the dangers in the use of eval(). There are probably many small hidden dangers just like everything else, but the two big risks - the reason why eval() is considered evil - are performance and code injection.
On to your specific case. From what I understand, you're generating the strings yourself, so assuming you're careful not to allow a string like "rm -rf something-important" to be generated, there's no code injection risk (but please remember, it's very very hard to ensure this in the general case). Also, if you're running in the browser then code injection is a pretty minor risk, I believe.
As for performance, you'll have to weight that against ease of coding. It is my opinion that if you're parsing the formula, you might as well compute the result during the parse rather than run another parser (the one inside eval()). But it may be easier to code using eval(), and the performance hit will probably be unnoticeable. It looks like eval() in this case is no more evil than any other function that could possibly save you some time.
eval()
并不是邪恶的。 或者,如果是的话,它是邪恶的,就像反射、文件/网络 I/O、线程和 IPC 在其他语言中是“邪恶的”一样。如果,为了您的目的,
eval()
比手动解释更快,或者使您的代码更简单或更清晰......那么您应该使用它。 如果两者都不是,那么你就不应该。 就那么简单。eval()
isn't evil. Or, if it is, it's evil in the same way that reflection, file/network I/O, threading, and IPC are "evil" in other languages.If, for your purpose,
eval()
is faster than manual interpretation, or makes your code simpler, or more clear... then you should use it. If neither, then you shouldn't. Simple as that.当你信任来源时。
对于 JSON,篡改源或多或少是困难的,因为它来自您控制的 Web 服务器。 只要 JSON 本身不包含用户上传的数据,使用 eval 就没有什么大的缺点。
在所有其他情况下,我会竭尽全力确保用户提供的数据符合我的规则,然后再将其提供给 eval()。
When you trust the source.
In case of JSON, it is more or less hard to tamper with the source, because it comes from a web server you control. As long as the JSON itself contains no data a user has uploaded, there is no major drawback to use eval.
In all other cases I would go great lengths to ensure user supplied data conforms to my rules before feeding it to eval().
让我们来真正的人:
现在每个主要浏览器都有一个内置控制台,您的潜在黑客可以充分使用它来调用具有任何值的任何函数 - 为什么他们会费心使用 eval 语句 - 即使他们可以 ?
如果编译 2000 行 JavaScript 需要 0.2 秒,那么如果我评估四行 JSON
甚至克罗克福德对“评估是邪恶的”的解释也是站不住脚的。
正如克罗克福德本人可能会说的那样“这种说法往往会产生非理性的神经症。不要相信它。”
理解 eval 并知道它何时可能有用更为重要。 例如,eval 是一个用于评估软件生成的服务器响应的实用工具。
顺便说一句:Prototype.js 直接调用 eval 五次(包括在 evalJSON() 和 evalResponse() 中)。 jQuery 在 parseJSON 中使用它(通过函数构造函数)。
Let's get real folks:
Every major browser now has a built-in console which your would-be hacker can use with abundance to invoke any function with any value - why would they bother to use an eval statement - even if they could?
If it takes 0.2 seconds to compile 2000 lines of JavaScript, what is my performance degradation if I eval four lines of JSON?
Even Crockford's explanation for 'eval is evil' is weak.
As Crockford himself might say "This kind of statement tends to generate irrational neurosis. Don't buy it."
Understanding eval and knowing when it might be useful is way more important. For example, eval is a sensible tool for evaluating server responses that were generated by your software.
BTW: Prototype.js calls eval directly five times (including in evalJSON() and evalResponse()). jQuery uses it in parseJSON (via Function constructor).
对于
eval()
,我倾向于遵循 Crockford 的建议,并避免使用它共。 即使看起来需要它的方式也不需要它。 例如,setTimeout()
允许您传递函数而不是 eval。即使它是一个可信源,我也不会使用它,因为 JSON 返回的代码可能是乱码,这充其量可能会做出一些奇怪的事情,最坏的情况是暴露出一些不好的东西。
I tend to follow Crockford's advice for
eval()
, and avoid it altogether. Even ways that appear to require it do not. For example, thesetTimeout()
allows you to pass a function rather than eval.Even if it's a trusted source, I don't use it, because the code returned by JSON might be garbled, which could at best do something wonky, at worst, expose something bad.
Eval 是对用于模板化代码的编译的补充。 我所说的模板化是指您编写一个简化的模板生成器,它可以生成有用的模板代码,从而提高开发速度。
我编写了一个框架,开发人员不使用 EVAL,但他们使用我们的框架,而该框架又必须使用 EVAL 来生成模板。
使用以下方法可以提高 EVAL 的性能; 您必须返回一个函数,而不是执行脚本。
应该组织成
Caching f肯定会提高速度。
Chrome 还允许非常轻松地调试此类功能。
关于安全性,使用或不使用eval几乎没有什么区别,
如果您的服务器端安全性足够可靠,任何人都可以从任何地方进行攻击,那么您不必担心 EVAL。 正如我提到的,如果 EVAL 不存在,攻击者就有很多工具可以侵入您的服务器,无论您的浏览器的 EVAL 功能如何。
Eval 仅适用于生成一些模板,以根据未使用的内容进行复杂的字符串处理提前。 例如,我更喜欢
As 而不是
As 我的显示名称,它可以来自数据库并且不是硬编码的。
Eval is complementary to compilation which is used in templating the code. By templating I mean that you write a simplified template generator that generates useful template code which increases development speed.
I have written a framework, where developers don't use EVAL, but they use our framework and in turn that framework has to use EVAL to generate templates.
Performance of EVAL can be increased by using the following method; instead of executing the script, you must return a function.
It should be organized as
Caching f will certainly improve the speed.
Also Chrome allows debugging of such functions very easily.
Regarding security, using eval or not will hardly make any difference,
If your server-side security is solid enough for anyone to attack from anywhere, you should not worry about EVAL. As I mentioned, if EVAL would not exist, attackers have many tools to hack into your server irrespective of your browser's EVAL capability.
Eval is only good for generating some templates to do complex string processing based on something that is not used in advance. For example, I will prefer
As opposed to
As my display name, which can come from a database and which is not hardcoded.
底线
如果您创建或清理了您
评估
的代码,那么它绝不会邪恶。稍微更详细一点,
如果在服务器上运行时使用客户端提交的输入运行,则
eval
是邪恶,而该客户端不是由开发人员创建的或者是 >未经开发商清理。如果在客户端上运行,
eval
是不是邪恶的,即使使用客户端制作的未经净化的输入。显然,您应该始终清理输入,以便对代码消耗的内容进行一定的控制。
推理
客户端可以运行他们想要的任何任意代码,即使开发人员没有编码; 这不仅适用于评估的内容,也适用于对 eval 本身的调用。
Bottom Line
If you created or sanitized the code you
eval
, it is never evil.Slightly More Detailed
eval
is evil if running on the server using input submitted by a client that was not created by the developer or that was not sanitized by the developer.eval
is not evil if running on the client, even if using unsanitized input crafted by the client.Obviously you should always sanitize the input, as to have some control over what your code consumes.
Reasoning
The client can run any arbitrary code they want to, even if the developer did not code it; This is true not only for what is evaled, but the call to
eval
itself.在 Chrome (v28.0.1500.72) 中调试时,我发现如果变量未在生成闭包的嵌套函数中使用,则它们不会绑定到闭包。 我想,这是 JavaScript 引擎的优化。
BUT:当在导致闭包的函数内部使用
eval()
时,所有外部函数的变量都会绑定到闭包,甚至如果根本不使用它们。 如果有人有时间测试是否会产生内存泄漏,请在下面给我留言。这是我的测试代码:
我想在这里指出的是,eval() 不一定引用本机
eval()
函数。 这完全取决于函数的名称。 因此,当使用别名调用本机eval()
时(比如var noval = eval;
),然后在内部函数noval(expression);
中, >) 那么当表达式
引用了应该是闭包一部分但实际上不是闭包一部分的变量时,它的求值可能会失败。When debugging in Chrome (v28.0.1500.72), I found that variables are not bound to closures if they are not used in a nested function that produces the closure. I guess, that's an optimization of the JavaScript engine.
BUT: when
eval()
is used inside a function that causes a closure, ALL the variables of outer functions are bound to the closure, even if they are not used at all. If someone has the time to test if memory leaks can be produced by that, please leave me a comment below.Here's my test code:
What I like to point out here is, that eval() must not necessarily refer to the native
eval()
function. It all depends on the name of the function. So when calling the nativeeval()
with an alias name (sayvar noval = eval;
and then in an inner functionnoval(expression);
) then the evaluation ofexpression
may fail when it refers to variables that should be part of the closure, but is actually not.我看到人们提倡不要使用 eval,因为它是邪恶的,但我看到同样的人动态地使用 Function 和 setTimeout,所以他们在幕后使用 eval :D
BTW,如果您的沙箱不够确定(例如,如果您正在允许代码注入的网站上工作),则 eval 是您的最后一个问题。 安全的基本规则是所有输入都是邪恶的,但是对于 JavaScript 甚至 JavaScript 本身也可能是邪恶的,因为在 JavaScript 中你可以覆盖任何函数并且你可以不确定您使用的是真实的,因此,如果恶意代码在您之前启动,您就不能信任任何 JavaScript 内置函数 :D
现在这篇文章的尾声是:
如果您真的< /strong> 需要它(80% 的时间不需要 eval)并且你确定自己在做什么,只需使用 eval (或更好的 Function ;) )、闭包和 OOP 覆盖80/90% 的情况 eval 可以使用另一种逻辑替换,其余的是动态生成的代码(例如,如果您正在编写解释器),并且正如您已经说过的评估 JSON(在这里您可以使用Crockford 安全评估 ;) )
I saw people advocate to not use eval, because is evil, but I saw the same people use Function and setTimeout dynamically, so they use eval under the hoods :D
BTW, if your sandbox is not sure enough (for example, if you're working on a site that allow code injection) eval is the last of your problems. The basic rule of security is that all input is evil, but in case of JavaScript even JavaScript itself could be evil, because in JavaScript you can overwrite any function and you just can't be sure you're using the real one, so, if a malicious code start before you, you can't trust any JavaScript built-in function :D
Now the epilogue to this post is:
If you REALLY need it (80% of the time eval is NOT needed) and you're sure of what you' re doing, just use eval (or better Function ;) ), closures and OOP cover the 80/90% of the case where eval can be replaced using another kind of logic, the rest is dynamically generated code (for example, if you're writing an interpreter) and as you already said evaluating JSON (here you can use the Crockford safe evaluation ;) )
唯一应该使用 eval() 的情况是当您需要动态运行动态 JS 时。 我说的是从服务器异步下载的 JS...
...10 次中有 9 次你可以通过重构轻松避免这样做。
The only instance when you should be using eval() is when you need to run dynamic JS on the fly. I'm talking about JS that you download asynchronously from the server...
...And 9 times of 10 you could easily avoid doing that by refactoring.
eval
很少是正确的选择。 虽然在许多情况下,您可以通过将脚本连接在一起并即时运行来完成您需要完成的任务,但您通常可以使用更强大且可维护的技术:关联数组表示法(obj[ "prop"]
与obj.prop
相同)、闭包、面向对象技术、函数式技术 - 请改用它们。eval
is rarely the right choice. While there may be numerous instances where you can accomplish what you need to accomplish by concatenating a script together and running it on the fly, you typically have much more powerful and maintainable techniques at your disposal: associative-array notation (obj["prop"]
is the same asobj.prop
), closures, object-oriented techniques, functional techniques - use them instead.在服务器端,eval 在处理外部脚本(例如 sql、influxdb 或 mongo)时非常有用。 可以在运行时进行自定义验证,而无需重新部署服务。
例如具有以下元数据的成就服务
然后允许,
通过 json 中的文字字符串直接注入对象/值,对于模板化文本很有用
通过
可以用作比较器,假设我们制定规则如何验证 CMS 中的任务或事件
缺点:
可能会出现错误如果没有经过充分测试,代码中可能会出现错误,并且会破坏服务中的事物。
如果没有
如果黑客可以在您的系统上编写脚本,那么您就完蛋了。
验证脚本的一种方法是将脚本的哈希值保存在安全的地方,以便您可以在运行之前检查它们。
验证脚本的一种方法是将
On the server side eval is useful when dealing with external scripts such as sql or influxdb or mongo. Where custom validation at runtime can be made without re-deploying your services.
For example an achievement service with following metadata
Which then allow,
Direct injection of object/values thru literal string in a json, useful for templating texts
Can be use as a comparator, say we make rules how to validate quest or events in CMS
Con of this:
Can be errors in the code and break up things in the service, if not fully tested.
If a hacker can write script on your system, then you are pretty much screwed.
One way to validate your script is keep the hash of your scripts somewhere safe, so you can check them before running.
Eval 并不是邪恶的,只是被滥用了。
如果您创建了进入其中的代码或者可以信任它,那就没问题。
人们一直在谈论用户输入对于 eval 来说并不重要。 好吧~
如果有用户输入发送到服务器,然后返回客户端,并且该代码将在 eval 中使用而不经过清理。 恭喜,您已经打开了潘多拉魔盒,用户数据可以发送给任何人。
根据评估的位置,许多网站都使用 SPA,并且评估可以使用户更轻松地访问应用程序内部,否则这并不容易。 现在,他们可以制作一个伪造的浏览器扩展,可以将其粘贴到该评估中并再次窃取数据。
只需弄清楚您使用 eval 的意义是什么。 当您可以简单地创建方法来执行此类操作、使用对象等时,生成代码并不是非常理想。
现在是一个使用 eval 的很好的例子。
您的服务器正在读取您创建的 swagger 文件。 许多 URL 参数都是以
{myParam}
格式创建的。 因此,您希望读取 URL,然后将它们转换为模板字符串,而无需进行复杂的替换,因为您有许多端点。 所以你可以做这样的事情。请注意,这是一个非常简单的示例。
Eval isn't evil, just misused.
If you created the code going into it or can trust it, it's alright.
People keep talking about how user input doesn't matter with eval. Well sort of~
If there is user input that goes to the server, then comes back to the client, and that code is being used in eval without being sanitized. Congrats, you've opened pandora's box for user data to be sent to whoever.
Depending on where the eval is, many websites use SPAs, and eval could make it easier for the user to access application internals that otherwise wouldn't have been easy. Now they can make a bogus browser extension that can tape into that eval and steal data again.
Just gotta figure what's the point of you using the eval. Generating code isn't really ideal when you could simply make methods to do that sort of thing, use objects, or the like.
Now a nice example of using eval.
Your server is reading the swagger file that you have created. Many of the URL params are created in the format
{myParam}
. So you'd like to read the URLs and then convert them to template strings without having to do complex replacements because you have many endpoints. So you may do something like this.Note this is a very simple example.
由于还没有人提到它,让我补充一下
eval
对于 Webassemble-Javascript 互操作非常有用。 虽然在页面中包含可供 WASM 代码直接调用的预制脚本当然是理想的选择,但有时这是不切实际的,您需要从 C# 等 WebAssembly 语言传入动态 Javascript 才能真正完成您需要做的事情。在这种情况下它也是安全的,因为您可以完全控制传入的内容。嗯,我应该说,它并不比使用 C# 编写 SQL 语句安全,也就是说,它需要小心完成(正确转义字符串等) .) 每当使用用户提供的数据来生成脚本时。 但有了这个警告,它在互操作情况下就有了明确的地位,并且远非“邪恶”。
Since no one has mentioned it yet, let me add that
eval
is super useful for Webassembly-Javascript interop. While it's certainly ideal to have pre-made scripts included in your page that your WASM code can invoke directly, sometimes it's not practicable and you need to pass in dynamic Javascript from a Webassembly language like C# to really accomplish what you need to do.It's also safe in this scenario because you have complete control over what gets passed in. Well, I should say, it's no less safe than composing SQL statements using C#, which is to say it needs to be done carefully (properly escaping strings, etc.) whenever user-supplied data is used to generate the script. But with that caveat it has a clear place in interop situations and is far from "evil".
就客户端脚本而言,我认为安全问题是一个有争议的问题。 加载到浏览器中的所有内容都可以进行操作,并且应该如此对待。 当有更简单的方法来执行 JavaScript 代码和/或操作 DOM 中的对象(例如浏览器中的 URL 栏)时,使用 eval() 语句的风险为零。
如果有人想操纵他们的 DOM,我会说走开。 防止任何类型的攻击的安全性始终是服务器应用程序的责任。
从实用的角度来看,在可以用其他方式完成事情的情况下使用 eval() 没有任何好处。 然而,在某些特定情况下应该使用 eval。 当这样的时候,它绝对可以完成,而不会有任何炸毁页面的风险。
As far as client script goes, I think the issue of security is a moot point. Everything loaded into the browser is subject to manipulation and should be treated as such. There is zero risk in using an eval() statement when there are much easier ways to execute JavaScript code and/or manipulate objects in the DOM, such as the URL bar in your browser.
If someone wants to manipulate their DOM, I say swing away. Security to prevent any type of attack should always be the responsibility of the server application, period.
From a pragmatic standpoint, there's no benefit to using an eval() in a situation where things can be done otherwise. However, there are specific cases where an eval SHOULD be used. When so, it can definitely be done without any risk of blowing up the page.
代码生成。 我最近编写了一个名为 Hyperbars 的库,它弥补了 virtual-dom 和 车把。 它通过解析车把模板并将其转换为 hyperscript 来实现此目的。 超脚本首先作为字符串生成,在返回它之前,使用
eval()
将其转换为可执行代码。 我发现在这种特殊情况下,eval()
与邪恶完全相反。基本上,在这种情况下
,
eval()
的性能也不是问题,因为您只需要解释生成的字符串一次,然后多次重复使用可执行输出。如果您好奇,您可以看到代码生成是如何实现的 在这里。
Code generation. I recently wrote a library called Hyperbars which bridges the gap between virtual-dom and handlebars. It does this by parsing a handlebars template and converting it to hyperscript. The hyperscript is generated as a string first and before returning it,
eval()
it to turn it into executable code. I have foundeval()
in this particular situation the exact opposite of evil.Basically from
To this
The performance of
eval()
isn't an issue in a situation like this too because you only need to interpret the generated string once and then reuse the executable output many times over.You can see how the code generation was achieved if you're curious here.
当您没有宏时,Eval 对于代码生成很有用。
举个(愚蠢的)例子,如果你正在编写一个 Brainfuck 编译器,你可能会想要构造一个以字符串形式执行指令序列的函数,并对其进行评估以返回一个函数。
Eval is useful for code generation when you don't have macros.
For (a stupid) example, if you're writing a Brainfuck compiler, you'll probably want to construct a function that performs the sequence of instructions as a string, and eval it to return a function.
我认为任何评估合理的案例都是罕见的。 您更有可能认为它是合理的而使用它,而不是在它实际上合理时才使用它。
安全问题是最为人所知的。 但也要注意 JavaScript 使用 JIT 编译,这对于 eval 来说效果很差。 Eval 对于编译器来说有点像黑匣子,JavaScript 需要能够(在某种程度上)提前预测代码,以便安全、正确地应用性能优化和范围界定。 在某些情况下,性能影响甚至会影响 eval 之外的其他代码。
如果您想了解更多:
https: //github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch2.md#eval
I think any cases of eval being justified would be rare. You're more likely to use it thinking that it's justified than you are to use it when it's actually justified.
The security issues are the most well known. But also be aware that JavaScript uses JIT compilation and this works very poorly with eval. Eval is somewhat like a blackbox to the compiler, and JavaScript needs to be able to predict code ahead of time (to some extent) in order to safely and correctly apply performance optimisations and scoping. In some cases, the performance impact can even affect other code outside eval.
If you want to know more:
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch2.md#eval
如果您可以完全控制传递给
eval
函数的代码,则可以使用它。It's okay to use it if you have complete control over the code that's passed to the
eval
function.如果可能的话,仅在测试期间。 另请注意,eval() 比其他专用 JSON 等评估器慢得多。
Only during testing, if possible. Also note that eval() is much slower than other specialized JSON etc. evaluators.
只要您可以确定代码的来源来自您或实际用户,就没有理由不使用 eval()。 尽管他可以操纵发送到 eval() 函数的内容,但这不是安全问题,因为他能够操纵网站的源代码,因此可以更改 JavaScript 代码本身。
那么...什么时候不使用 eval() ? 仅当第三方有可能更改 Eval() 时,才不应使用 Eval()。 就像拦截客户端和服务器之间的连接一样(但如果这是一个问题,请使用 HTTPS)。 您不应该使用 eval() 来解析其他人(例如在论坛中)编写的代码。
There is no reason not to use eval() as long as you can be sure that the source of the code comes from you or the actual user. Even though he can manipulate what gets sent into the eval() function, that's not a security problem, because he is able to manipulate the source code of the web site and could therefore change the JavaScript code itself.
So... when to not use eval()? Eval() should only not be used when there is a chance that a third party could change it. Like intercepting the connection between the client and your server (but if that is a problem use HTTPS). You shouldn't eval() for parsing code that is written by others like in a forum.
如果确实需要 eval 并不是邪恶的。 但是我偶然发现的 99.9% 的 eval 用法都是不需要的(不包括 setTimeout 的东西)。
对我来说,邪恶不是性能问题,甚至不是安全问题(好吧,间接地两者都是)。 所有这些不必要的 eval 使用都会增加维护难度。 重构工具被抛弃了。 搜索代码很困难。 这些评估带来的意想不到的影响是巨大的。
If it's really needed eval is not evil. But 99.9% of the uses of eval that I stumble across are not needed (not including setTimeout stuff).
For me the evil is not a performance or even a security issue (well, indirectly it's both). All such unnecessary uses of eval add to a maintenance hell. Refactoring tools are thrown off. Searching for code is hard. Unanticipated effects of those evals are legion.
我相信 eval 对于客户端 Web 应用程序来说是一个非常强大的函数,并且安全......与 JavaScript 一样安全,但事实并非如此。 :-) 安全问题本质上是服务器端问题,因为现在,使用 Firebug 这样的工具,您可以攻击任何 JavaScript 应用程序。
My belief is that eval is a very powerful function for client-side web applications and safe... As safe as JavaScript, which are not. :-) The security issues are essentially a server-side problem because, now, with tool like Firebug, you can attack any JavaScript application.
JavaScript 的 eval() 什么时候不是邪恶的?
我总是试图阻止使用 eval。 几乎总是有更干净、更可维护的解决方案可用。 即使 JSON 解析也不需要 Eval 。 Eval 增加了维护地狱。 道格拉斯·克罗克福德(Douglas Crockford)等大师对它不屑一顾,这并非没有道理。
但我发现了一个应该使用它的示例:
当您需要传递表达式时。
例如,我有一个构造通用
google.maps.ImageMapType
对我来说对象,但我需要告诉它秘诀,它应该如何从zoom
和coord
参数构建图块 URL:When is JavaScript's eval() not evil?
I'm always trying to discourage from using eval. Almost always, a more clean and maintainable solution is available. Eval is not needed even for JSON parsing. Eval adds to maintenance hell. Not without reason, it is frowned upon by masters like Douglas Crockford.
But I found one example where it should be used:
When you need to pass the expression.
For example, I have a function that constructs a general
google.maps.ImageMapType
object for me, but I need to tell it the recipe, how should it construct the tile URL from thezoom
andcoord
parameters:我使用
eval
的示例:导入。通常是如何完成的。
但在 eval 和一些辅助函数的帮助下,它看起来更好了:
importable 可能看起来像(这个版本不支持导入具体成员)。
My example of using
eval
: import.How it's usually done.
But with the help of
eval
and a little helper function it gets a much better look:importable
might look like (this version doesn't support importing concrete members).尽管在许多情况下,您可以通过将脚本连接在一起并即时运行来完成您需要完成的任务,但您通常可以使用更强大且可维护的技术。 eval 很少是正确的选择。:关联数组表示法(obj["prop"] 与 obj.prop 相同)、闭包、面向对象技术、函数技术 - 请改用它们。
While there may be numerous instances where you can accomplish what you need to accomplish by concatenating a script together and running it on the fly, you typically have much more powerful and maintainable techniques at your disposal. eval is rarely the right choice.: associative-array notation (obj["prop"] is the same as obj.prop), closures, object-oriented techniques, functional techniques - use them instead.
当您使用解析函数(例如 jQuery.parseJSON)解析 JSON 结构时,它期望 JSON 文件具有完美的结构(每个属性名称都用双引号引起来)。 然而,JavaScript 更灵活。 因此,您可以使用 eval() 来避免它。
When you parse a JSON structure with a parse function (for example, jQuery.parseJSON), it expects a perfect structure of the JSON file (each property name is in double quotes). However, JavaScript is more flexible. Therefore, you can use eval() to avoid it.