是否存在字符串s
这样
(new Function(s))();
和
eval(s);
表现不同?我正在尝试“检测”如何评估字符串。
最佳答案
检查 arguments
目的。如果它存在,那么您就在函数中。如果不是,则为 eval
编。
请注意,您必须检查 arguments
在 try...catch
像这样 block :
var s = 'try {document.writeln(arguments ? "Function" : "Eval") } catch(e) { document.writeln("Eval!") }';
(new Function(s))();
eval(s);
Demo
Solution to
nnnnnn
's concern.为此,我编辑了 eval 函数本身:var _eval = eval;
eval = function (){
// Your custom code here, for when it's eval
_eval.apply(this, arguments);
};
function test(x){
eval("try{ alert(arguments[0]) } catch(e){ alert('Eval detected!'); }");
}
test("In eval, but it wasn't detected");
关于javascript - 如何检测我在 eval() 调用中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227546/