Testing with ActionScript 3 Dictionary Object
Post by efox | Date: 2008-07-29
The "Dictionary Object" is a new kind of Object in Actionscript 3, which lives in the flash.utils package. What is the difference between "Dictionary Object" and "Object" ? Here is my test:
Use "Dictionary Object" to associate a value with an object key:
var dict = new Dictionary();
var obj1 = new Object();
var obj2 = new Object();
var key:Object = new Object();
key.toString = function() { return “key” }
dict[key] = “Letters1″;
dict[obj1] = “Letters1″;
dict[obj2] = “Letters2″;
trace([dict[key],dict[obj1],dict[obj2]]); //Letters1,Letters1,Letters2
trace(dict[key] == dict[obj1]); //true
trace(obj1 == key); //false
Use "Object" to associate a value with an object key:
var dict = new Object();
var obj1 = new Object();
var obj2 = new Object();
var key:Object = new Object();
key.toString = function() { return “key” }
dict[key] = “Letters1″;
dict[obj1] = “Letters1″;
dict[obj2] = “Letters2″;
trace([dict[key],dict[obj1],dict[obj2]]); //Letters1,Letters2,Letters2
trace(dict[key] == dict[obj1]); //false
trace(obj1 == key); //false
From the output result, the object key can be saved as diffrent type of value in a "Dictionary Object". the object key of "Object" can only be string data.
If you want to implement hashMap in ActionScript 2, you need to use two arrays, then excute a search function for each time. now you can use "Dictionary Object" directly.





Previous
Next
Tags: