如何在javascript中覆盖eval函数? [英] How override eval function in javascript?

查看:152
本文介绍了如何在javascript中覆盖eval函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

(function() {
      var proxied = window.eval;
      window.eval = function() {
        return proxied.apply(this, arguments);
      };
    })();

但此代码无效。

推荐答案

你做不到。 (有一种有限的方式,但它非常有限,并且不能保持 bobince谈论。)

You can't. (There is a limited way of doing it, but it's quite limited and doesn't maintain the magic that bobince talks about.)

eval 在至少一个主要实现中不是真正的JavaScript函数(IE的JScript,至少不是通过IE7;还没有测试过新的IE8版本),所以马上就要开始了遇到麻烦,因为你将无法通过申请来调用原件(不是那对 eval )。

eval isn't a real JavaScript function in at least one major implementation (IE's JScript, at least not through IE7; haven't tested the new IE8 version), so right off the bat you're going to run into trouble, because you won't be able to call the original via apply (not that that really matters for eval).

最近的ECMAScript 5规范明确禁止在严格模式下覆盖 eval (不是说你是在那里使用严格模式),这让我怀疑非常好的理由没有覆盖它。

The recent ECMAScript 5 specification specifically disallows overriding eval in strict mode (not that you're using strict mode there), which makes me suspect that there are very good reasons for not overriding it.

这篇关于如何在javascript中覆盖eval函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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