I'm trying to execute a piece of code within an empty isolated execution context in JavaScript. In the below sample, I'm trying isolate isolated execution scope. What I want to do is to execute a function in context where no global variables are in.
(function() {
'use strict';
var scope = Object.create(null);
var isolated = function() {
'use strict';
console.log(document); // Trying to get undefined
// but traces `document`.
};
isolated.call(scope);
})();
I thought it was simple to nullify global variables but there are too many!
var isolated = function(window, document, location /* etc */) {
// ...
};
isolated.call(scope, undefined, undefined, undefined /* etc */);
Is there a better way to do this?
with(context) { ... }, so that any new variables we assign will go into the context object, not the global / window object. More info here: stackoverflow.com/a/62350258/218294