Assigning One Variable’s Value to Another
The result of assigning the one variable’s value to another variable depends on the type of value being assigned. It is absolutely crucial to understand the differeneces below, or else, eventually, you 'll find yourshelf in deep problems, trust me!
In an assignment where the source variable’s value is an instance of String, Boolean, Number, int, or uint, ActionScript makes a copy of that value and assigns the copy to the destination variable. After the assignment, two separate copies of the original value exist in system memory—the original value itself, and the copy of that value. The source variable points, or refers, to the original value in memory. The destination variable refers to the new value in memory.
By contrast, in an assignment where the source variable’s value is an instance of a custom class or an instance of a built-in class other than String, Boolean, Number, int, or uint, ActionScript associates the second variable directly with the first variable’s value. After the assignment, only one copy of the value exists in memory, and both variables refer to it. The variables are said to share a reference to the single object in memory. As a natural consequence, changes to the object made through the first variable are reflected by the second variable (and vice versa).
For example, consider the following code, which creates two local variables, a and b, and then assigns a’s value to b:
var a = new MyClass("Name01"); var b = a;
When the first line of the preceding code runs, ActionScript creates a new MyClass object, stores that object in memory, and then associates the local variable a with that object. When the second line of the preceding code runs, ActionScript associates the local variable b with the MyClass object already referred to by a. Changes made to the MyClass object through a are, hence, naturally reflected by b, and vice versa. For example, if we assign name using the code b.name= "Name02", then subsequently retrieving a.name also yields “Name02”. Or, if we assign name using the code a.name= "Name03", then subsequently retrieving b.name also yields “Name03”.