<html>
<body>
<script type="text/javascript">
document.write("<br />" + eval("2+2"));
</script>
</body>
</html>
这给我输出 4。
如果我删除 eval 函数并运行相同的东西,它会给我相同的输出,即 4
<html>
<body>
<script type="text/javascript">
document.write("<br />" + (2+2);
</script>
</body>
</html>
最佳答案
Eval 允许您执行存储在变量中的命令:
<script type="text/javascript">
var cmd = "2+2";
document.write("<br />" + eval(cmd));
</script>
关于javascript - Javascript 中的 eval 函数有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6546216/