calling eval() in particular context

前端 未结 14 1120
说谎
说谎 2020-11-27 17:05

I have following javaScript \"class\":

A = (function() {
   a = function() { eval(...) };
   A.prototype.b = function(arg1, arg2) { /* do something... */};
}         


        
14条回答
  •  佛祖请我去吃肉
    2020-11-27 17:58

    Actually you can accomplish this with an abstraction via a function:

    var context = { a: 1, b: 2, c: 3 };
    
    function example() {
        console.log(this);
    }
    
    function evalInContext() {
        console.log(this);        //# .logs `{ a: 1, b: 2, c: 3 }`
        eval("example()");        //# .logs `{ a: 1, b: 2, c: 3 }` inside example()
    }
    
    evalInContext.call(context);
    

    So you call the function with the context you want and run eval inside that function.

    Oddly, this seems to be working for me locally but not on Plunkr!?

    For a succinct (and arguably succulent ;) version you can copy verbatim into your code, use this:

    function evalInContext(js, context) {
        //# Return the results of the in-line anonymous function we .call with the passed context
        return function() { return eval(js); }.call(context);
    }
    

    EDIT: Don't confuse this and "scope".

    //# Throws an error as `this` is missing
    console.log(evalInContext('x==3', { x : 3}))
    
    //# Works as `this` is prefixed
    console.log(evalInContext('this.x==3', { x : 3})) 
    

    While one could do this:

    function evalInScope(js, contextAsScope) {
        //# Return the results of the in-line anonymous function we .call with the passed context
        return function() { with(this) { return eval(js); }; }.call(contextAsScope);
    }
    

    to bridge the gap, it's not what OP's question asked and it uses with, and as MDN says:

    Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the "Ambiguity Contra" paragraph in the "Description" section below for details.

    But it does work and isn't too "bad" (whatever that means), so long as one is aware of the oddnessess that can arise from such a call.

提交回复
热议问题