AS1 OOP: Object Basics
         by senocular  

Objects Are References
One important thing to be aware of with objects is that object variables are references to objects. As references, object variables aren't necessarily direct representations of values but rather pointer reference to the object in as it exists in Flash's memory.

[ object variables "point" to objects ]

One way to think of them is computer shortcuts (or aliases for Mac folk). Think about a shortcut to Flash on your computer's desktop. Double-clicking on the shortcut will open Flash as it references the actual Flash application file. However, you can rename the shortcut whatever you want without effecting Flash itself. Also, you can copy the shortcut 20 times in 20 different locations on your hard drive and that Flash application file will still be the only ‘real' Flash file remaining in the same place its always been. Each one of those shortcut copies will still reference that same one Flash application despite their being a copy or being located in some other directory.

Object variables are the same way. You can make new variables with different names equaling the same referenced object. The thing is, when referencing variables within an object, you have to remember each one of those object variables that point to that object will all be accessing the same values within that object. So, if you change a value through one object reference, it affects them all. Consider the following

firstObject = new Object();
firstObject.num = 1;
secondObject = firstObject;
trace(secondObject.num); // traces 1

 
secondObject.num = 2;
trace(secondObject.num); // traces 2
trace(firstObject.num); // traces 2

When firstObject is created, its saved as a reference to the new Object made in memory. Using firstObject to reference that object, num is added and given a value of 1. Then, secondObject is defined to be the value of firstObject. This makes secondObject also a reference to the same object. Both firstObject and secondObject are shortcuts to that object. You can see that the num value in secondObject exists and has the same value as before. When secondObject is used to change num to 2, the results can be seen in firstObject as well, since, they are in fact, again, just references to the very same object. This means, as you can pretty well see, copies of objects in this way aren't exactly copies. Only the reference is copied and not the object itself. If you ever need to copy an object, you need to create an entirely new object and manually copy each property from the original as a new property in the copy.

[ two variables, one object ]

Also, because objects are references, when you check for equality/inequality with either the == or != operators, you will be checking against the references themselves and not the equality of the object they reference. The firstObject and secondObjects from before are equal. They references the same object, so as references, they are the same. However, if you had two separate objects each with a num property of the same value, they will not be equal.

firstObject = {num: 1};
secondObject = firstObject;
trace(secondObject == firstObject); // traces true

 
firstObject = {num: 1};
secondObject = {num: 1};
trace(secondObject == firstObject); // traces false

Though the contents of firstObject and secondObject are the same, they are themselves not equal as they reference separate objects. The reference isn't representative of value but of a memory address . You can think of it as the address to a house in a neighborhood of similar houses. Though 2 houses can look exactly the same and even have the same furniture in them, they are not the same house. Their addresses will be different as they are located in different locations in the neighborhood, just as objects are located in different places in Flash's memory. The only time object references equal each other is when the addresses match. Then you are dealing with the same house and therefore the same object.

Prev Page
 



SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.