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


Variables — primitive types and reference types



Download 0.78 Mb.
View original pdf
Page12/16
Date04.04.2022
Size0.78 Mb.
#58550
1   ...   8   9   10   11   12   13   14   15   16
chp15
15.2.9 Variables — primitive types and reference types
When one begins to work with collections of values (e.g. with arrays or objects, one needs to be aware of the variable's value. A variable containing a primitive type value is straightforward for example consider the numeric variable age in this code var age = 21; However, the situation is not so simple when we consider an array variable. For example the ageList array of three ages defined as follows var ageList = new Array 3 ); ageList[0] = 5; ageList[1] = 3; ageList[2] = 11; It is not the case that ageList is the name fora single location in memory, since we know that this array is storing three different values. The location in memory named ageList is actually a reference to the place in memory where the first value in the array can be found. The diagram below attempts to illustrate this:
Note
There is nothing special about the locations 001727 etc, these numbers have been made up and included to illustrate unnamed locations in memory. So we can think of the variable ageList as referring to where the array values can be found. The implication of the different between variables of primitive types and reference types is rather important,


9 especially in the case of copying or overwriting the values of reference variables.

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