简体   繁体   中英

Swap two global JS variables inside function

I want to swap two globals variable values inside a function.

Here is what I have tried so far. For technical reasons, I can't change the values outside the function . My script contains a lot of lines of code but I can't change my var s:

var One = 10
var Two = 20
var Three = 30

function altStats(originaleValue, alternativeValue)
{
  // Swap logic goes here...
}

altStats(One, Two);

console.log(One); // should be 20
console.log(Two); // should be 10
console.log(Three); // should be 30

Arguments are always passed by value in JavaScript. You could pass an object in and do the swap.

If these variables are off of window you could swap them around that way.

If I really can write only inside the function like you said:

 var One = 10 var Two = 20 var Three = 30 function altStats(originaleValue, alternativeValue) { var app=[One,Two,Three], result=[]; app.forEach(function(variable){ if(variable===originaleValue) result.push(alternativeValue); else if(variable===alternativeValue) result.push(originaleValue); else result.push(variable); }); One=result[0]; Two=result[1]; Three=result[2]; } altStats(One, Two); console.log(One); // 20 console.log(Two); // 10 console.log(Three); // 30 

ECMAScript 6 solution

A clever trick would be to use [One, Two] = [Two, One] . It swaps the values in one line, using an array. Try it:

 var One = 10; var Two = 20; var Three = 30; [One, Two] = [Two, One]; console.log("One: "+One); // 20 console.log("Two: "+Two); // 10 console.log("Three: "+Three); // 30 

Legacy solution (without using a function)

The above snippet will only work in recent browsers, as suggested in the comments. Therefore, a legacy solution would be:

 var One = 10; var Two = 20; var Three = 30; One = [Two, Two = One][0]; console.log("One: "+One); // 20 console.log("Two: "+Two); // 10 console.log("Three: "+Three); // 30 

In this version, what we do is we use an array of two values, the second one passes the value of One to Two , and the first one is the original value of Two . From that array we grab the 1st [0] element and set the value of One . A bit confusing, but if you take a minute, you will get it.

Legacy solution (using a function)

Finally, you could use a function to do this, one that takes an array as an argument, swaps its values and returns the array. Calling the function, using [One, Two] and returning the values to [One, Two] would accomplish the same as the above two solutions:

 var One = 10; var Two = 20; var Three = 30; function altStats(values) { var temp = values[0]; values[0] = values[1]; values[1] = temp; return values; } [One, Two] = altStats([One, Two]); console.log("One: " + One); // 20 console.log("Two: " + Two); // 10 console.log("Three: " + Three); // 30 

Trying to do a swap within a function only passing in variables to your arguments will not work. Use an object. Or don't use a function, for the variables are kept within function scope.

 var One = 10 var Two = 20 var Three = 30 var temp = One; One = Two; Two = temp; console.log(One); // 20 console.log(Two); // 10 console.log(Three); // 30 

Another way you can do this is by passing an object to your function

function swap(obj, original, toSwap) {
  var temp = obj[original];
  obj[original] = obj[toSwap];
  obj[toSwap] = temp;
  return obj;
}

var test = {ONE: 10, TWO: 20}
var res = swap(test, 'ONE', 'TWO');
console.log(res); //{ONE: 20, TWO: 10}

As long as the values you want to swap are numbers, you can use this approach that won't require a temporary variable:

One = One + Two; //One = 30, Two = 20
Two = One - Two; //One = 30, Two = 10
One = One - Two; //One = 20, Two = 10

Won't make any relevant difference in almost any case, but it's worth knowing that it's possible.

There is already an accepted answer at the time I write this, but it's a hack and not reliable (see my comment on the answer).

So the answer is, you can't do what you want generically. Since JS does not pass arguments by reference but by value, you need to introduce a reference yourself in order to be able to pass it around, eg:

 var values = { One: 10, Two: 20, Three: 30 } function altStats(values, propX, propY) { var tmp=values[propX]; values[propX]=values[propY]; values[propY]=tmp; } altStats(values, "One", "Two"); console.log(values.One); // 20 console.log(values.Two); // 10 console.log(values.Three); // 30 

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