Is there a way to disable some variables for an eval?
var test = 'example';
eval('console.log(test)'); // here I would like test null
console.log(test); // here test should display 'example'
Is there a way to disable some variables for an eval?
var test = 'example';
eval('console.log(test)'); // here I would like test null
console.log(test); // here test should display 'example'
Wrap your
evalcall inside an IIFE and shadow outer variables with local variables:The
evalcode will only have access to the IIFE's localtest, not thetestdeclared inside ofdoSomething.Note, however, that global variables cannot be shadowed in this way, since they will be accessible as properties of
windoworglobal. You could shadowwindow(orglobal) and deny access to all global variables as properties ofwindow.