calling eval() in particular context
javascript
Solution
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.
However, ESM enables strict mode by default, which you can circumvent with the `Function` constructor like this:
"use strict";
function evalInScope(js, contextAsScope) {
return new Function(`with (this) { return (${js}); }`).call(contextAsScope);
}
console.log(evalInScope("a + b", { a: 1, b: 2 })); // 3
Problem
I have following javaScript "class": ``` A = (function() { a = function() { eval(...) }; A.prototype.b = function(arg1, arg2) { /* do something... */}; })(); ``` Now let's assume that in eval() I'm passing string that contains expression calling b with some arguments: ``` b("foo", "bar") ``` But then I get error that b is not defined. So my question is: how to call eval in context of class A?