Learn As3:delete keywords and class members

Source: Internet
Author: User
Tags garbage collection object object reference variable
The keyword delete keyword is used in flash to delete a defined variable, but does not remove the object from memory (this is the work of the garbage collector). It simply sets the reference of a variable to be invalid so that the object in memory can no longer be referenced and used, and can no longer be enumerated using for.

In fact, the garbage processor (GC) will automatically remove variables from memory that are no longer referenced and used at specific times. For example, you create two object references A and B, all point to a reference to the object Objectx, and if you delete a, you won't let the garbage collector remove the objectx from memory because B's reference still points to the object. If you delete both A and B, there is no longer a reference to Objectx, and Objectx will be reclaimed by the garbage collector. For example:

var a:object = new Object ();
var b:object = A; B and a refer to the same new Object ();
Delete A;
Trace (b); Output [Object]-exists in memory or
Delete B;
GC will Reclaim object This feature is almost the same in Flash8 and 9 (AS123), but in Flash8, some of the GC features are improved and work better. (Note that garbage collection is not instantaneous)

Although GC has no fundamental change in AS3, the behavior of the DELETE keyword changes because of the use of a new virtual machine. The DELETE keyword can now only be valid for the dynamic properties of the class and for Non-public members. In AS1 and 2, delete can be used on all things.

ActionScript 2
Class Deletevarclass {

public Var Myvar:number;

function Deletevarclass () {
MyVar = 1;
Trace (MyVar); 1
Delete MyVar;
Trace (MyVar); Undefined
}
}

ActionScript 3
Package {
public class Deletevarclass {

public Var Myvar:number;

Public Function Deletevarclass () {
MyVar = 1;
Trace (MyVar); 1
Delete MyVar;
Trace (MyVar); 1
}
}
In the AS3 example above, because the MyVar variable is a public member, you cannot delete the variable with delete.

Although you cannot delete a class member in AS3, if you want to delete all references to an object, you can replace the delete by setting the variable to null. Such as:

MyVar = null; if all references to an object are NULL,GC, the object will automatically be deleted from memory.

*dictionary class

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 a key that is not a string as a key value pair. 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 the new Object (), which is passed to the string "[Object Object]"

for (var prop:string in obj) {
Trace (prop); Output: [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]" as the key, thus pointing to the same data. For example:

ActionScript Code:
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
}dictionary class will not have this restriction, you can set the key to any one 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]
Trace (Dict[prop]); Traces:1, 2
Although in trace, the output is [object], but the result is the result of the object's ToString. In the Dictionary object, a different object reference is represented.

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

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.