In the following code, I need the variable some_var not to be renamed or removed by Google's Closure Compiler.
function scopedEval(code){
var some_var = 'world' ;
return eval('('+code+')') ;
}
scopedEval('alert("hello, "+some_var)') ;
The code to be eval-ed relies on the existence of a few variables, so I need them to be left untouched.
How can I instruct Closure Compiler to do that?
PS:
Please disregard the issue about the use of eval being a bad practice. That's another matter entirely.
There might be some Closure Compiler options allowing this sort of thing specifically, but failing that, I would tackle the problem one of these two ways:
Option 1
Create a global object to store your variables, and then use the
js_externsoption to prevent it from being munged:Then you can apply string properties to this object that should be left untouched
Option 2
If for some reason
vars['some_var']won't work for you and you need to usesome_varliterally inside theevaled code, then you can usewithto get around this. In this case, you don't need to declarevarsas an extern.I'll leave it up to your discretion whether or not you feel dirty using the two features of JavaScript that attract the most vitriol together, namely
withandeval.