The dictionary class in AS3

Source: Internet
Author: User

The Dictionary Class (Flash.utils.Dictionary) in AS3 is a new as class. The only difference between the dictionary class and object is that the Dictionary object can use non-string keys as key-value pairs. For example:

var obj:object = new Object ();
obj["name"] = 1; The key is the string "name"
OBJ[1] = 2; Key is 1 (converted to string "1")
Obj[new Object ()] = 3; The key is a new Object () and is transferred to the string "[Object Object]"

for (var prop:string in obj) {
Trace (prop); Output: [Object Object], 1, name
Trace (Obj[prop]); Output: 3, 2, 1
}

That is, no matter what type of variable is used as the key, it is converted to a string. Also, if you use a different object as a key, it will be converted to the string "[Object Object]" as the key, and therefore point to the same data. For example:

var a:object = new Object ();
var b:object = new Object ();

var obj:object = new Object ();
Obj[a] = 1; Obj["[Object Object]"] = 1;
OBJ[B] = 2; Obj["[Object Object]"] = 2;

for (var prop:string in obj) {
Trace (prop); Traces: [Object Object]
Trace (Obj[prop]); Traces:2
}

The dictionary class will not have this restriction, and you can set the key to any data type. For example:

Import Flash.utils.Dictionary;

var a:object = new Object ();
var b:object = new Object ();

var dict:dictionary = new Dictionary ();
Dict[a] = 1; Dict[a] = 1;
DICT[B] = 2; DICT[B] = 2;

for (Var prop:* in dict) {
Trace (prop); Traces: [Object Object], [Object Object]
Trace (Dict[prop]); Traces:1, 2
}

Attention:

1, although in the trace, the output is still [object object], but this result is the result of the object's ToString. In the Dictionary object, a different object reference is represented.

2. The type of prop here is *. This is important because the keys in the Dict object may be of any data type.



Here is a summary of the differences between dictionary and object:

1. The key of object must be a string, or an expression (variable or function) that represents a string, or an expression that is not a string, then the ToString () method is called to convert it to a string;

2. Unlike object, the key used by dictionary is a reference to an object, not a string, and non-primitive object key calls are judged by the equality operator (= = =), and the data type is not cast after casting.


Object keys and memory management

Adobe Flash Player uses a garbage collection system to recover memory that is no longer in use. When an object does not have a reference to it, it can be garbage collected, and the memory will be restored the next time the garbage collection system is executed. For example, the following code creates a new object and assigns a reference to this object to the variable myObject:
var myobject:object = new Object ();

As long as there is a reference to this object, the garbage collection system does not recover the memory occupied by this object. If you change the value of myObject so that it points to another object or set it to a value of NULL, and there is no other reference to the original object, you can garbage recycle the memory that the original object occupies.

If you use MyObject as a key in a Dictionary object, another reference to the original object is created. For example, the following code creates two object references (the MyObject variable and the keys in the MyMap object):
Import Flash.utils.Dictionary;
var myobject:object = new Object ();
var mymap:dictionary = new Dictionary ();
Mymap[myobject] = "Foo";

For the object referenced by MyObject to be garbage collected, you must remove all references to it. In this case, you must change the value of MyObject and remove the MyObject key from MyMap, as shown in the following code:
MyObject = null;
Delete Mymap[myobject];

In addition, it can use the useweakreference parameter of the Dictionary constructor to make the keys of all dictionaries a "weak reference". The garbage collection system ignores weak references, which means that objects with only weak references can be garbage collected. For example, in the following code, to enable the object to be garbage collected, you do not need to remove the MyObject key from MyMap:
Import Flash.utils.Dictionary;
var myobject:object = new Object ();
var mymap:dictionary = new Dictionary (true);
Mymap[myobject] = "Foo";
MyObject = null; Enables the object to be garbage collected.

The dictionary class in AS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.