sameold
sameold

Reputation: 19242

variable change and reset logic

I'm noticing that I'm doing this a lot in my code.

I need to store the value of a variable so I can change it and restore it again. Let's say the variable is var1. I need to use the variable under the same name var1 which means I need to make the value changes to it directly. But I don't want to lose the original value. So I store it somewhere var1_original, make the change to it, run the code I need to run, then restore the original value.

  var var1_original = var1;
  var1 = 'new value'; //I have to store the new value under the same name (var1) 

  //have some 
  //lines of code
  //here

  var1 = var1_original;

So, I need to do this on many variables (var1, color, dimArr, etc.) and of course the code that gets executed in the middle is different. Is there a way to create a function that would do saving and restoring, but still let the code in the middle run?

Upvotes: 0

Views: 430

Answers (4)

Tgr
Tgr

Reputation: 28170

This is the best you can get generally:

window.savedValues = {}
function cache(name, values) {
    if (typeof values == undefined) {
        return window.savedValues[name];
    } else {
        window.savedValues[name] = values;
    }
}

cache('foo', [var1, color, dimArr]);
// ...
var saved = cache('foo');
var1 = saved[0];
// ....

Some modern browsers support destructuring assingments, which makes the restoring part more convenient:

[var1, color, dimArr] = cache('foo');

Though I can't help but feel that something has gone horribly wrong if you need such a thing in the first place.

Upvotes: 1

KooiInc
KooiInc

Reputation: 122916

Not sure if i see what you mean. I think you don't need to store things. You can create a new variable and use that

var var1 = 'somevalue', var2 = 'anothervalue';
// do things using `var2`
// continue using 'var1'

Or, if the code must use var1 as variable name, use a immediately invoked function to give it its own scope:

var var1 = 'somevalue';
//...
(function(){
  var var1 = 'othervalue';
  // do things to var
}());
//...
//continue with original var1

Upvotes: 1

nrabinowitz
nrabinowitz

Reputation: 55678

This probably isn't the answer you want, but you could wrap the inner block in an anonymous function:

var1 = "original value";
(function(var1) {
    var1 += "... another value";
})(var1);
var1; // "original value"

That puts the inner block into a separate variable scope and allows you to reuse the var1 variable name without changing it in the outer block. If you don't need to start with the original value in the inner block, you don't need to pass it in - you can just declare a new var1 as a new variable with a var statement in the inner block.

Upvotes: 2

Mikola
Mikola

Reputation: 9326

You should probably be making a new variable instead of reusing the old one. Otherwise it could get very confusing if the same name means to different things within the same straight-line block of code.

Upvotes: 0

Related Questions