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
eval
call inside an IIFE and shadow outer variables with local variables:The
eval
code will only have access to the IIFE's localtest
, not thetest
declared inside ofdoSomething
.Note, however, that global variables cannot be shadowed in this way, since they will be accessible as properties of
window
orglobal
. You could shadowwindow
(orglobal
) and deny access to all global variables as properties ofwindow
.