Specify global context in JavaScript
global-variables, javascript
Solution
The closest thing you can do is mask the global variables using a `with` statement.
var myGlobalContext = {bar: "foo"};
with(myGlobalContext)
{
console.log(bar);
}
This is not the same as changing the global context, because other globals that aren't found in `myGlobalContext` will still exist.
In general, the `with` statement is bad, but it sounds like your use case might be one where it makes sense.
Problem
In JavaScript, is it possible to specify the global context which will be used if a local variable isn't defined? Example: ``` (function foo() { console.log(bar); })(); ``` It will actually print `window.bar`. Can I somehow change the global context? Something like this: ``` var myGlobalContext = { bar: "foo" }; (function foo() { console.log(bar); }).applyWithGlobal(myGlobalContext); ``` It should print `myGlobalContext.bar`. Or attach `this` to be the global context? I hope the example is clear enough.