简体   繁体   中英

Replace a global variable temporarily in a local scope

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!

Every global can be accessed using window object, with a little changing your code here is example

a = {something: "Safe", other: "Foo"}
function hello(){
  var a = window.a.other; // Foo
  console.log(window.a.something); // Safe
}
a.something; // Safe
a.other; // Foo

hello();

or

a = {something: "Safe", other: "Foo"}
function hello(){
  var a = this.a.other; // Foo
  delete a;
  console.log(this.a.something); // Safe
}
a.something; // Safe
a.other; // Foo

hello();

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).

a = {something: "Safe", other: "Foo"}
function hello(b){
  var window = null;
  var a = b; // a.other;
  a.something; // Undefined
}
a.something // Safe
a.other // Foo

hello(a.other)

(I never really said what could or couldn't go outside the function).

Let's say it's a and its scope is global.

You mean "a is a global variable".

... 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.

Impossible. You can create a variable with the same name that is on a scope chain, however you can't conditionally create properties of variable objects (ie the objects used for identifier resolution on the scope chain). You can only declare local varaibles, which means they exist before any code is run and so can't be conditional, or you can assign directly to an undeclared identifier at which point it becomes a global variable.

[snipped code]

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;

The code doesn't in any way "clear" the value of a . It creates a local variable a so that the identifier a will resolve to that variable, not to the global a . To differentiate betweent the two, you can access the global a as a property of the global object:

var a = 'whatever';
var myFunction = (function(global) {
  return function() {
    var a;    // local a
    global.a; // global a
  }
}(this));

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.

Impossible, though it might be almost possible in ES5 strict mode provided the code attempting to access the global a is inside another function and can't get a reference to the global object.

But I don't think you can guarantee that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM