Naming Array Elements


Most array elements are numbered, but they can also be named. Naming array elements is an easy way to keep information organized within an array. None of these named elements can be manipulated by array methods, nor can they be seen when the array is traced.

There are two ways to create named array elements. The first uses dot syntax, and the second uses brackets and string literals. Here's an example of both methods:

NOTE

When you use dot syntax with any other object in Flash, the word that follows the dot is referred to as a property. But because the Array is a unique class of Object, when you use dot syntax, the elements are called Named Array Elements.


 var my_array:Array = new Array(); my_array.fName = "David"; my_array ["age"] = 25; trace(my_array); //output: (nothing) 

We first created an empty array to hold the named elements and then we attached the first element using dot syntax and set it equal to a string. Then we attached the next named element using brackets and a string to name it, and we set its value to a number. Finally, we traced the array, but there were no results. This is because, as previously stated, when you trace an array, named elements will not appear. You have to call the named elements individually. Therefore, using the same code as before, we will trace both named elements individually when tracing the array:

 var my_array:Array = new Array(); my_array.fName = "David"; my_array ["age"] = 25; trace(my_array ["fName"]); trace(my_array.age); //output: David //        25 

This time, when we traced the elements individually, the trace was successful.

Named array elements will also not show up in the array's length. Here's an example:

 var my_array:Array = new Array(); my_array.fName = "David"; trace(my_array.length); //output: 0 

Now that you know how to add elements to an array, let's cover how to remove them.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net