Flash / Flex / ActionScript/Array/Dictionary — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 12:19, 26 мая 2010
Содержание
Iterate through the key-value pairs of an object with the for..in and for each..in loops.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var notes:Object = {t: 2, g: 14, s: 9}; for (var name:String in notes) { trace("Notes on " + name + ": " + notes[name]); } } }
}
</source>
Using an Object as a Lookup Key with Dictionaries
<source lang="java">
package{
import flash.display.Sprite; import flash.utils.* public class Main extends Sprite{ public function Main(){ var notes:Dictionary = new Dictionary(); var city:String = "New York City"; var currentConditions:String = "light showers"; var currentTemp:Number = 56; notes[city] = "Central Park"; notes[currentTemp] = "20"; notes[currentConditions] = "70% chance of precipitation"; trace("Current Weather for", city, notes[city]); trace("Current Temperature :", currentTemp, "degrees", notes[currentTemp]); trace("Current Conditions :", currentConditions, "(", notes[currentConditions],")"); } }
}
</source>
When using delete on an item in an Array, the length property is not updated.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var pastas:Object = {t: 2, g: 14, s: 9}; trace(pastas.length); // Displays 9 delete pastas["s"]; trace(pastas.length); // Displays undefined } }
}
</source>
Working with Associative Arrays
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var lotto:Array = new Array(12, 30, 42, 6, 29, 75); lotto.description = "This week"s lotto picks."; trace(lotto[3]); // Displays: 6 trace(lotto["description"]); // Displays: This week"s lotto picks. } }
}
</source>