There is no good way to do this in javascript itself (but see Gareth Hayes answer for another option).
There are several bad ways.
(function() { var scope = Object.create(null); var obscurer = {}; for (var key in this) { obscurer[key] = undefined; } with (obscurer) { var isolated = function() { 'use strict'; console.log(document); }; } isolated.call(scope); })();
Note that you really get the error message because the console is not defined, not the document, although you can fix this without blocking the “console” in the “unscientific” object. You will probably find that you need a whole group of more global variables than you realize.
You also block the enumerated properties of the window. If you learn about immeasurable properties that you want to block, you will have to add them to the dark.
Of course, using with means that you can no longer use strict mode, and everyone will look down at you with their noses.
There are more interesting options if you are working in node and not in a browser.
source share