eval()
executes a string, and does it in the current scope. But what, if I need to make my own eval-like function, which will modify given string of code (not necessary synchronously), and then execute it? I obviously should use eval in it, except I can't, because eval will use scope of my function, instead of someone's who will call my function. Is there a way to force eval to use the caller's scope?
In this simple example I want to make function eval2, which will behave exactly like eval. Code which will "clone" any other single-param function, doesn't work for eval.
function eval2(code){
return eval(code)
}
(() => {
let x = 1;
let code = `console.log(x)`
eval(code) // 1
eval2(code) // ReferenceError: x is not defined
})()
The only way I see to perform this, is to always put second parameter as callback, which will call eval for the given code. Even so it will not be posible to create local variables (reading is enough for my purposes). Also, of course, it looks terrible, and I would be glad, if there is a way to make same function, which will be nicer to use.
Awful method:
function eval2(code, callback){
// ... do something with code
callback(code)
}
(() => {
let x = 1;
let code = `console.log(x)`
eval2(code, code => eval(code))
})()
PS:
From everything I found, the most related to my problem is this:
JavaScript extending the eval() function?
But suggested solution there only allows to save/read from the global object, and doesn't allow to read locals.
Anyway I'm wondering why binding eval to something changes its behavior.