
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Equality and Inequality Operators
|
123
two distinct arrays. The references are therefore different, and the comparison evalu-
ates to
false:
nameList1 = ["Linkovich", "Harris", "Sadler"];
nameList2 = ["Linkovich", "Harris", "Sadler"];
// The two arrays are not considered equal, so
// the trace() statement doesn't execute.
if (nameList1 = = nameList2) {
trace("They're the same");
}
In this example, cities and canadianCities both refer to the same array, so they are
equal:
canadianCities = ["Toronto","Montreal","Vancouver"];
cities = canadianCities;
// The two arrays are considered equal, so
// the trace() statement does execute.
if (cities = = canadianCities) {
trace("They're the same");
}
In this example, myFirstBall and mySecondBall have the same constructor (i.e., are
both objects derived from the same class), but they exist as separate (unequal)
instances:
function Ball ( ) {
// ...initialization code goes here...
}
myFirstBall = new Ball( );
mySecondBall = new Ball( );
myFirstBall = = mySecondBall // false
As shown in Chapter 3, composite data values are compared by reference, not by
value.
To duplicate an array’s contents without copying the array reference, we can use the
Array.slice() method. In this example, we copy the elements from the
dishes array
into
kitchenItems:
dishes = [ "cup", "plate", ...