如何检测我在eval()调用中? [英] How can I detect I'm inside an eval() call?

查看:96
本文介绍了如何检测我在eval()调用中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在字符串 s ,以便

(new Function(s))();

eval(s);

表现不同?我正试图检测字符串的评估方式。

behave differently? I'm trying to "detect" how a string is being evaluated.

推荐答案

检查参数对象。如果它存在,那么你就在这个功能中。如果不是,它已经 eval ed。

Check for the arguments object. If it exists, you're in the function. If it doesn't it has been evaled.

请注意,您必须支票参数 try ... catch 这样的块中:

Note that you'll have to put the check for arguments in a try...catch block like this:

var s = 'try {document.writeln(arguments ? "Function" : "Eval") } catch(e) { document.writeln("Eval!") }';
(new Function(s))();
eval(s);

演示

解决 nnnnnn 关注。为此,我编辑了eval函数本身:

Solution to nnnnnn's concern. For this, I've edited the eval function itself:

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");​

这篇关于如何检测我在eval()调用中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆