Availability: |
| ||||||||
JavaScript syntax: | - | delete anObject | |||||||
- | delete myArray[anIndex] | ||||||||
- | delete myObject.aProperty | ||||||||
Argument list: | anIndex | The index of the item to be deleted from the array | |||||||
anObject | An object to be deleted | ||||||||
aProperty | An object property to be removed |
The delete operator is used to delete a property from an object or delete a reference to an object. It can also be used to delete an element from an array.
Using the new operator, a new object is created with a reference count of zero. Assigning that object creation to a variable increments the reference count. Saving it as an object property also increments the reference count. Storing it in an array does likewise.
All of these are simply references to the same object.
Deleting a variable containing a reference to an object decrements the reference count for that object.
The associativity is from right to left.
Refer to the operator precedence topic for details of execution order.
The delete operator will not work with MSIE version 3.02 or earlier. It also won't work in Netscape 2.02.
Netscape does not generate an error in version 3.0 if the delete operator is used but it is functionally ignored and does nothing.
In JavaScript version 1.0 and version 1.1, the delete operator does not actually delete any object properties. Instead it sets them to null. If you then subsequently test for the existence of those properties, they might not prove false when you expect them to.
In the JavaScript 1.2 implementation, in version 4 of Netscape , the behavior of the delete operator as it applies to variables is slightly different. Any variable declared with the var statement is considered to be permanent and cannot be deleted. This will generate an error if you try. This happens for the case when you enclose the script in <SCRIPT LANGUAGE="JAVASCRIPT1.2"> tags.
There is some difference between Netscape and MSIE as to whether global variables can be deleted. The safest assumption is that they cannot, so to remain portable, your script should not try.
// Create a new object myObject = new Object; // Then delete a reference to it delete myObject;
ECMA 262 edition 2 - section - 11.1.4
ECMA 262 edition 2 - section - 11.4.1
ECMA 262 edition 3 - section - 11.4.1
Wrox Instant JavaScript - page - 21
Wrox Instant JavaScript - page - 28
Prev | Home | Next |
DEL.dateTime | Up | Delete() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |