Before actually asking anything, I’ll go ahead and say this is a theoretical question; however, it might be implemented on a website later on.
Anyway, I have a variable, any variable. Let’s say it’s a and its scope is global. Now, for a specific function, I want to set that variable’s value to something other than it’s global value, but based on it, and without changing its value globally. For example:
a = {something: "Safe", other: "Foo"}
function hello(){
var a = a.other; // Foo
a.something; // Undefined
}
a.something; // Safe
a.other; // Foo
The issue with the above code is that when I define var a in the function, it will have already cleared the value of the global a locally before setting it; in other words, it would return something like Can't access property [other] of undefined [a].
Again, a should still be a (so using another variable name is not an option, or at least not the ideal one). In fact, the global a should not be accessible from the function hello.
Edit: window will also be overwritten with null, regarding Milan Jaric’s answer.
Thanks in advance!
This is what I was looking for…now, before you think I had the answer before I asked, I didn’t, I was only able to reach a tangible solution based on Milan Jaric’s answer (thanks btw).
(I never really said what could or couldn’t go outside the function).