代码之家  ›  专栏  ›  技术社区  ›  adamJLev

如何执行“eval”而不在JavaScript中写入“eval”

  •  7
  • adamJLev  · 技术社区  · 16 年前

    YUI compressor 如果发现“eval”语句,则不会完全压缩代码,因为担心它会破坏其他语句。

    所以基本上问题是,有没有其他(可能是创造性的)方法来编写返回eval函数的表达式? 我试了几次,但没有掷骰子:

    window['eval'](stuff);
    window['e'+'val'](stuff);
    // stuff runs in the global scope, we need local scope
    
    this['eval'](stuff);
    // this.eval is not a function
    
    (new Function( "with(this) { return " + '(' + stuff + ')' + "}"))() 
    // global scope again
    

    有什么想法吗?

    7 回复  |  直到 16 年前
        1
  •  3
  •   Miquel    16 年前

    var x = 5;
    
    var f = new Function('alert(this.x)');
    
    function A(x){
        this.x = x;
        f.apply(this,[]);
    }
    
    a = new A(10);
    

    当f与A一起应用时,此警报将向10发出

        2
  •  3
  •   Mickael Lherminez hyankov    6 年前

    感谢所有的想法,我最终只是在输出JS的构建脚本中进行了文本替换,基本上是在所有内容都被压缩后,用EVAL替换$EVAL$。我希望使用纯JS的方式,但是有这么多不同的eval浏览器实现,最好还是不要使用eval

    但根据迪米特尔的回答和一些胡闹,我发现了以下几点。 似乎这个['eval']不起作用的原因是因为它发生的地方,在MooTools JSON.decode中,也是散列中的一个:

    var JSON = new Hash({
      // snip snip
      decode: function(string, secure) {
        if ($type(string) != 'string' || !string.length) return null;
        if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null;
    
        return this.eval('(' + string + ')'); // Firefox says: TypeError: this.eval is not a function
      }
    });
    

    var TOP = this;
    var JSON = new Hash({
      // snip snip
      decode: function(string, secure) {
        if ($type(string) != 'string' || !string.length) return null;
        if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null;
    
        return TOP.eval('(' + string + ')'); // All good, things run within the desired scope.
      }
    });
    

    然而,这在Safari中不起作用,所以底线是,我试图做的事情不能跨兼容地完成。eval是一个特殊的敏感功能,每个浏览器都会对其进行不同的处理。

        3
  •  1
  •   recursive    16 年前

    是否可以重构对某些外部垫片函数的eval调用,而这些函数不是压缩文件的一部分?

        4
  •  1
  •   Dimitar Christoff    16 年前

    我错过什么了吗?

    var noteval = this.eval; // can be defined before the file is loaded
    noteval("alert('not eval. at all');");
    
    (function() {
        console.log(this);
        noteval("alert('chavs!');");
    }).bind(window)();
    
    (function() {
        console.log(this);
        noteval("alert('crappy parents');");
    }).bind(window.parent)();
    

    检查一下 http://www.jsfiddle.net/nGL79/

    并且特定于mootools:

    window["ev"+"al"].pass("alert('what');")();
    this["ev"+"al"].pass("alert('no!');")(); // local scope too?
    

    var noteval = window["ev"+"al"].create({
        bind: this
    }); 
    

    希望这能对你有所帮助。。。希望你不会有麻烦 函数eval必须直接调用,而不是通过另一个名称的函数调用 虽然

        5
  •  0
  •   elcuco    16 年前
    var e = "e";
    window[e+"val"](stuff);
    
        6
  •  0
  •   Nick Berardi    16 年前
        7
  •  -1
  •   Nathan Tuggy TonyLuigiC    9 年前

    这种方式需要jQuery。

    function NotEval(code, callBack) {
        $.ajax({
            url: 'data:application/javascript;charset=utf-8,' + encodeURIComponent(code),
            cache:true,
            success: function (r) {
                if (typeof callBack === "function") {
                    callBack()
                }
            },
            error: function (r) {
                console.log("Eval error");
                console.log(r)
            }
        })
    }
    
    推荐文章