Don\'t understand why the code below is overwriting my var arr. Any help appreciated.
var arr = [1,2,3]
var string = \"function swap(arr) { var newarr = arr; v
JavaScript kind of has a thing where it creates references instead of copies. This can be useful, and annoying:
Where is it useful?
var self = this;
is a commonly used trick to trap the scope of a function.
var elem = document.getElementById('my_element');
is a way refer to elements as variables
var arr = [1, 2, 3, 4];
var arr2 = arr; //arr2 now references arr
var arr2[1] = 'd'; //Modifies arr2 and reference
console.log(arr2); //Line 6
console.log(arr); //Line 7
This will give us:
[1, 'd', 3, 4] (line 6)
[1, 'd', 3, 4] (line 7)
In conclusion, this is just regular behavior of JavaScript!
To solve this, add
var arr2 = arr.slice();
.slice()
returns a new version of the array. You can write your own function to do this by:
Array.prototype.clone = function () {
return this.slice();
}
Now we can do:
var arr = [1, 2, 3, 4];
var arr2 = arr.clone(); // Returns cloned arr
var arr2[1] = 'd'; // Modifies arr2
console.log(arr2); //Line 6
console.log(arr); //Line 7
This time:
[1, 'd', 3, 4] (line 6)
[1, 2, 3, 4] (line 7)