There is a need to perform an anonymous function (on the client) to a limited scope with its own environment variables (without access to the global variables). Overriding context this is not a problem, but with variable I see only one option - a eval. But as eval should be left to the edge, I decided to ask the community if there are other solutions to the problem.
Actually the problem:
function sandbox(fn){ // with eval
fn = fn.toString();
// need to dynamically create variables inside fn
fn = '(function(){"use strict"; var window, document; (' + fn + ').call(this);}) ';
fn = eval(fn);
fn.call({}); // example
}
sandbox(function(){
console.log(window, document, this); // return undefined, undefined, Object {}
});
For a redefinition of the variables must respond to the function sandbox(). I mean the one with a clear redefinition of variables within functions are not transmitted to offer. Dining with the dynamic creation of the frame with its own environment, as well as transferring variable parameters to a function
(function(window, document){...})(null, null)
also not suitable for it, and so everything is clear, and is needed here dynamization of this process by the sandbox(). There are other ways to solve without using eval?
Thanks in advance.