Is there any way to change a variable while out of scope? I know in general, you cannot, but I’m wondering if there are any tricks or overrides. For example, is there any way to make the following work:
function blah(){
var a = 1
}
a = 2;
alert(blah());
EDIT (for clarification):
The hypothetical scenario would be modifying a variable that is used in a setInterval function which is also out of scope and in an un-editable previous javascript file. It’s a pretty wacky scenario, but it’s the one I intend to ask about.
No. No tricks or overrides. You have to plan to have both places be able to see the variable in the same scope.
The only trick I can think of regarding scope is using
windowin a browser to get to the global object. This can help you get to a “hidden” variable–one that’s in scope but whose name has been overtaken by a local variable (or other variable closer in the scope chain).Closures and classes can afford you some other tricks with scope, but none that allow you to override the scoping rules entirely.