Or “How to Shoot Yourself in the Foot even Easier”
Saving the best for last, there is the ever-dreaded…
Script segments passed to
eval execute inside the current scope, so they can access and modify
everything, including all variables that are visible at that position in code!
let script = "x += 32"; let x = 10; eval(script); // variable 'x' is visible! print(x); // prints 42 // The above is equivalent to: let script = "x += 32"; let x = 10; x += 32; print(x);
eval can also be used to define new variables and do other things normally forbidden inside
a function call.
let script = "let x = 42"; eval(script); print(x); // prints 42
Treat it as if the script segments are physically pasted in at the position of the