De-I > delete

delete

Syntax

delete (reference);

Arguments

reference The name of variable or object to eliminate.

Description

Operator; destroys the object or variable specified as the reference, and returns true if the object was successfully deleted; otherwise returns false. This operator is useful for freeing up memory used by scripts, although, delete is an operator, it is typically used as a statement:

delete x;

The delete operator may fail and return false if the reference does not exist, or may not be deleted. Predefined objects and properties, and variables declared with var, may not be deleted.

Player

Flash 5 or later.

Example

The following example creates an object, uses it, and then deletes it once it is no longer needed:

account = new Object();
	account.name = 'Jon';
	account.balance = 10000;
	...
	delete account;

The following example deletes a property of an object:

// create the new object "account"
account = new Object();
// assign property name to the account 
	account.name = 'Jon'; 
// delete the property
delete account.name; 

The following is another example of deleting an object property:

// create an Array object with length 0
array = new Array(); 
// Array.length is now 1
	array[0] = "abc";
// add another element to the array,Array.length is now 2
	array[1] = "def"; 
// add another element to array,Array.length is now 3
	array[2] = "ghi";
// array[2] is deleted, but Array.length is not changed,
	delete array[2]; 

The following example illustrates the behavior of delete on object references:

// create a new object, and assign the variable ref1 to refer to the object
ref1 = new Object();
ref1.name = "Jody";
// copy the reference variable into a new variable, and delete ref1
ref2 = ref1;
delete ref1;

If ref1 had not been copied into ref2, the object would have been deleted when we deleted ref1, because there would be no references to it. If we were to delete ref2, there would no longer be any references to the object, and it would be destroyed and the memory it was using would be made available.

See also

var