Chapter 15. JavaScript 4: Objects and Arrays Table of Contents


Copying reference type variables



Download 0.78 Mb.
View original pdf
Page14/16
Date04.04.2022
Size0.78 Mb.
#58550
1   ...   8   9   10   11   12   13   14   15   16
chp15
15.2.11 Copying reference type variables
Things are different when one is working with variables of reference types. Consider the following code, where first an array called myArray is created with some initial values, next a variable called thing2 is assigned the value of myArray: var myArray = new Array 6, 3, 5, 1 ); var thing = myArray; Since myArray is a reference to the array values in memory, what has been copied into thing2 is the reference — so now both myArray and thing2 are referring to the same set of values in memory The implications are that if a change is made to the array values, since both myArray and thing are referring to the same values in memory, both will be referring to the changed array.
Exercise 2


10 What is the value of myArray[2] after the following statements have been executed var myArray = new Array 6, 3, 5, 1 ); var thing = myArray; thing = 27; thing = 19; thing = 77;

Download 0.78 Mb.

Share with your friends:
1   ...   8   9   10   11   12   13   14   15   16




The database is protected by copyright ©ininet.org 2024
send message

    Main page