Javascript callback function unexpected behavior

I would like to understand why situation 1 and situation 2 do not return the same results.

Situation 1:

var array1 = ["1", "2", "3"];
var array2 = array1.reverse();

console.log(array1); // ["3", "2", "1"]
console.log(array2); // ["3", "2", "1"] Why this doesn't work ?

Situation 2:

var array1 = ["1", "2", "3"];
var array2 = array1;

console.log(array1);           // ["1", "2", "3"]
console.log(array2.reverse()); // ["3", "2", "1"] Why this works ?
+4
source share
8 answers

AT Situation 1

The inverse method transfers the elements of the object of the calling array into place, mutates the array, and returns a reference to the array.

In Situation 2you have the same link. You print the array array1 before .

var array1 = ["1", "2", "3"];
var array2 = array1;

console.log(array1);           // [ '1', '2', '3' ]
console.log(array2.reverse()); // [ '3', '2', '1' ]
console.log(array1);           // [ '3', '2', '1' ]
+10
source

When you call .reverse, you change the value and then return an array.

var array1 = ["1", "2", "3"]; //Create a new array
var array2 = array1.reverse();//Reverse array1 and assign it to array2

, array1, array2, - , .

+6

reverse() . . MDN Array.prototype.reverse()

var array1 = ["1", "2", "3"];
var array2 = array1;             // point to the same object

console.log(array1);             // original array1 (["1", "2", "3"])
console.log(array2.reverse());   // reverses array2 and array1 since they
                                 //   point to the same object and returns
                                 //   array2 (["3", "2", "1"])
console.log(array1);             // check that array1 was reversed (["3", "2", "1"])
console.log(array1.reverse() === array1);   // returns true
+4

, Array.reverse() :

  • , , .
  • , .

/

1

var array1 = ["1", "2", "3"];  // Creates new array
var array2 = array1.reverse(); // Reverse array1 and stores reference to array1 in array2

// Since array1 and array2 point to the same object, they will have
// the same values since they are pointing to the same object
console.log(array1); // ["3", "2", "1"]
console.log(array2); // ["3", "2", "1"]

2

var array1 = ["1", "2", "3"];  // Creates new array
var array2 = array1;           // array2 now holds a reference to array1

// Same as the previous example, array1 and array2 both have the
// same values since they are pointing to the same object    
console.log(array1);           // ["1", "2", "3"]

// Now we reverse array2, which reverses array1 AND array2
console.log(array2.reverse()); // ["3", "2", "1"]

// array1 is now also reversed
console.log(array1);           // ["3", "2", "1"]

array2.reverse() array1 array2 , .

+2

, reverse() . , var array2 = array1.reverse(), 1 . Array1 , array2 . .

var array2 = array1 , . 1, . 2 . . 1, , array1 array2 .

+1

(demo)

var array1 = ["1", "2", "3"];
var array2 = array1.slice(0).reverse();

console.log(array1, array2);

slice(0) .

+1

Array is an object in javascript, so assigning an array to another array just assigns its reference, so that both of them will point to the same object. If for this situation it is better to use the array slice function to copy the array data to another, for example

var array1 = ["1", "2", "3"];
var array2 = array1.slice().reverse();

console.log(array1); // ["1", "2", "3"]
console.log(array2); // ["3", "2", "1"]
0
source

All Articles