简体   繁体   中英

Javascript Koans - array reference

I have problem understand a section of the array references in Javascript koans:

it("should know array references", function () {
  var array = [ "zero", "one", "two", "three", "four", "five" ]; 
  var assignedArray = array;
  assignedArray[5] = "changed in assignedArray"; 
  expect(array[5]).toBe('changed in assignedArray');

in here, why does changing assignedArray[5] affects the array? Through my understanding, assignedArray would only reference to the array and change assigned array would not affect the array itself.

The value of a variable holding an object is a reference to that object. That is, there is an array in memory somewhere and both array and assignedArray refer to that same location. The statement assignedArray = array assigns the value of array -- a reference to the array -- to assignedArray so that they both refer to the same thing.

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