是否有充分的理由使用eval()? [英] Is there ever a good reason to use eval()?
问题描述
在我看来,eval()
与goto一样受到鄙视.所谓eval
,我的意思是一个将字符串作为代码执行的函数,如PHP,Python,JavaScript等所示.是否曾经有过使用eval()
合理的情况(perl除外)?如果没有,为什么要用这么多语言来实现呢?
It seems to me that eval()
is treated with the same disdain that goto is. And by eval
, I mean a function for executing a string as code, as seen in PHP, Python, JavaScript, etc. Is there ever a situation where using eval()
is justified (except perl)? And if not, why do so many languages implement it?
推荐答案
是-当没有其他方法可以在合理的代码行数内以合理的清晰度完成给定任务时.
Yes - when there is no other way to accomplish the given task with a reasonable level of clarity and within a reasonable number of lines of code.
这消除了99%的情况下在所有语言和上下文中使用eval
的情况.
This eliminates 99% of cases where eval
is used, across the board in all languages and contexts.
这篇关于是否有充分的理由使用eval()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!