Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我没记错 eval 在给定字符串中执行有效代码
eval("alert('hey')");
和
setTimeout("alert('hey')",1000);
做同样的事情,只是用一个计时器。设置超时和评估一样有风险吗?
我会说你听到了同样的反对意见。setTimeout(带有字符串而不是函数参数)与 eval 几乎相同。
如果可能的话,
setTimeout(function(){ alert ("hey") ; }, 1000);
因为当人们说“eval”时,他们的意思是“eval 和任何或多或少等同于 eval 的函数”,但前者的说法要短得多。所以你的问题的答案是肯定的,它同样有风险。