Matt Huntington 8 years ago
parent 02ba95c104
commit f890cfa1d5

@ -745,47 +745,3 @@ var y = false;
console.log(x,y);
```
<details><summary>Note</summary>
The reason this is special and new is because we have to
remember that array values are normally passed by reference.
It is also unusual to have an array that is not assigned to a variable
```JavaScript
// Pass by reference
var a = 1;
var b = 2;
var originalArray = [a,b];
console.log('is `a` equal to orginalArray[0]:', a === originalArray[0]);//true
var newArray = originalArray;
//will reverse BOTH arrays (because it is actually two references to the same array)
newArray.reverse()
console.log('This is newArray.reverse():', newArray)
console.log('This is originalArray after newArray has been reversed', originalArray)
console.log('is `a` equal to orginalArray[0]:', a === originalArray[0]);//false
```
To make a duplicate array that is not passed by reference, you would have to do something like:
```JavaScript
var a = 1;
var b = 2;
var anotherOriginalArray = [a,b];
console.log('is `a` equal to anotherOrginalArray[0]:', a === anotherOriginalArray[0]);//true
var trueNewArray = anotherOriginalArray.map(function(e){
return e;
});
console.log('this is trueNewArray', trueNewArray);
trueNewArray.reverse();
console.log('\nThis is trueNewArray.reverse():', trueNewArray);
console.log('This is anotherOriginalArray after trueNewArray has been reversed', anotherOriginalArray);
console.log('is `a` equal to anotherOriginalArray[0]:', anotherOriginalArray[0] === a); //true
```
</details>

Loading…
Cancel
Save