I currently have two files:
file1.js
class helper {
static wrap(code) {
eval(`console.log("start"); ${code} console.log("stop");`);
}
}
file2.js
import { helper } from "./file1.js"; //(using typescript on nodejs)
function somethingCool() {
console.log("middle");
}
helper.wrap("somethingCool();");
This will not work because somethingCool
is not in the scope of file1. Is there anyway to get the eval to use the scope of where the function was called as opposed to the local scope?
The only way I have come up with is to pass an anonymous function as an anchor like this:
helper.wrap("somethingCool();", (val) => eval(val));
but that isn't very clean looking and would require the end developer to constantly add that anchor.