I want to be able to dynamically define variables (i.e x = 1, y = 2
- but where the user defines the names x and y, and may have a "z") - and have them be in scope when executing javascript from a string (i.e. with eval()).
Obviously, this can be done statically like so:
var x = 1, y = 2;
eval('x + y');
But I want something like this:
var context = {};
userDefinedVariables.forEach(({name, value}) => context[name] = value);
eval('x + y'); //somehow call with context
Many answers suggest concatenating strings - like eval('var ' + names[0] + ' = ' + values[0])
but that's prone to errors and code injection.
Many answers suggest modifying the global window and running eval in the global context.
How can I create a new local context for my eval execution that has access to my variable names? Something like eval().call(context)