Core JavaScript 1.5 Reference:Operators:Special Operators:delete Operator
From MDC
Contents |
[edit] Summary
The delete operator deletes an object, an object's property, or an element at a specified index in an array.
| Operator | |
| Implemented in: | JavaScript 1.2, NES3.0 |
| ECMA Version: | ECMA-262 |
[edit] Syntax
delete expression
where expression should evaluate to a property reference, e.g.:
delete variableName delete objectExpression.property delete objectExpression["property"] delete objectExpression[index] delete property // legal only within a with statement
If expression does not evaluate to a property, delete does nothing.
[edit] Parameters
-
objectName - The name of an object.
-
property - The property to delete.
-
index - An integer representing the array index to delete.
[edit] Description
The fifth form is legal only within a with statement, to delete a property from an object.
You can use the delete operator to delete variables declared implicitly but not those declared with the var statement.
If the delete operator succeeds, it removes the property from the object entirely, although this might reveal a similarly named property on a prototype of the object.
Some object properties cannot be deleted. In the ECMA 262 specification these are marked as DontDelete.
The delete operator returns false only if the property exists and cannot be deleted. It returns true in all other cases.
x = 42; // assigns as property of global object
var y = 43; // declares as variable
myobj = new Number()
myobj.h = 4 // create property h
myobj.k = 5 // create property k
delete x // returns true (can delete if declared implicitly)
delete y // returns false (cannot delete if declared with var, property is DontDelete)
delete Math.PI // returns false (cannot delete most predefined properties, declared DontDelete)
delete myobj.h // returns true (can delete user-defined properties)
with(myobj) {
delete k; // returns true (equivalent to delete myobj.k)
}
delete myobj // returns true (can delete if declared implicitly, equivalent to x)
You cannot delete a property on an object that it inherits from a prototype (although you can delete it directly on the prototype).
function Foo(){}
Foo.prototype.bar = 42;
var foo = new Foo();
delete foo.bar; // but doesn't do anything
alert(foo.bar); // alerts 42, property inherited
delete Foo.prototype.bar; // deletes property on prototype
alert(foo.bar); // alerts "undefined", property no longer inherited
[edit] Deleting array elements
When you delete an array element, the array length is not affected. For example, if you delete a[3], a[4] is still a[4] and a[3] is undefined. This holds even if you delete the last element of the array (delete a[a.length-1]).
When the delete operator removes an array element, that element is no longer in the array. In the following example, trees[3] is removed with delete.
trees=new Array("redwood","bay","cedar","oak","maple")
delete trees[3]
if (3 in trees) {
// this does not get executed
}
If you want an array element to exist but have an undefined value, use the undefined value instead of the delete operator. In the following example, trees[3] is assigned the value undefined, but the array element still exists:
trees=new Array("redwood","bay","cedar","oak","maple")
trees[3]=undefined
if (3 in trees) {
// this gets executed
}