Resum
L'operador delete
elimina una propietat d'un objecte.
Sintaxi
delete expression
On expression s'avaluaria com a una referència a una propietat, p.ex.:
delete object.property delete object['property']
Paràmetres
object
- El nom d'un objecte, o una expressió que es resol com un objecte.
property
- La propietat a eliminar.
Què retorna
En mode estricte llença un error si la propietat no és configurable (retorna false
en el mode no estricte). Retorna true
en tots els altres casos.
Descripció
Al contrari del que comunment es pensa, l'operador delete
no té res a veure amb l'alliberament de memòria directament (només ho fa indirectament a través de trencar les referencies. Vegeu la pàgina de gestió de memòria per més detalls).
Si l'operador delete
té èxit, elimina la propietat de l'objecte per complet. No obstant això, si existeix una propietat amb el mateix en la cadena de prototip de l'objecte, l'objecte heretarà aquesta propietat del prototipus.
delete
és només efectiu en propietats d'un objecte. No té cap efecte en variables o en noms de funcions. Si bé de vegades hi ha assignacions mal caracteritzades com a variables globals, assignacions que no especifiquin un objecte (e.g. x = 5
) són assignacions de propietats de l'objecte global.
delete
no pot eliminar algunes propietats d'objectes predefinits (com Object, Array, Math etc). Aquests són descrits a partir de ECMAScript 5 cap endavant com a no configurables.
Temporal dead zone
The "temporal dead zone" (TDZ), specified in ECMAScript 6 for const
and let
declarations, also applies to the delete
operator. Thus, code like the following will throw a ReferenceError
.
function foo() { delete x; let x; } function bar() { delete y; const y; }
Exemples
x = 42; // creates the property x on the global object var y = 43; // creates the property y on the global object, and marks it as non-configurable myobj = { h: 4, k: 5 }; // x is a property of the global object and can be deleted delete x; // returns true // y is not configurable, so it cannot be deleted delete y; // returns false // delete doesn't affect certain predefined properties delete Math.PI; // returns false // user-defined properties can be deleted delete myobj.h; // returns true // myobj is a property of the global object, not a variable, // so it can be deleted delete myobj; // returns true function f() { var z = 44; // delete doesn't affect local variable names delete z; // returns false }
If the object inherits a property from a prototype, and doesn't have the property itself, the property can't be deleted by referencing the object. You can, however, delete it directly on the prototype.
function Foo(){} Foo.prototype.bar = 42; var foo = new Foo(); // returns true, but with no effect, // since bar is an inherited property delete foo.bar; // logs 42, property still inherited console.log(foo.bar); // deletes property on prototype delete Foo.prototype.bar; // logs "undefined", property no longer inherited console.log(foo.bar);
Deleting array elements
When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.
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
.
var trees = ["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:
var trees = ["redwood","bay","cedar","oak","maple"]; trees[3] = undefined; if (3 in trees) { // this gets executed }
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
1a edició d'ECMAScript. | Estàndard | Definició inicial. Implementat en JavaScript 1.2 |
ECMAScript 5.1 (ECMA-262) The definition of 'The delete Operator' in that specification. |
Standard | |
ECMAScript 6 (ECMA-262) The definition of 'The delete Operator' in that specification. |
Release Candidate |
Compatibilitat amb navegadors
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Temporal dead zone | ? | 36 (36) | ? | ? | ? |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Temporal dead zone | ? | ? | 36.0 (36) | ? | ? | ? |
Cross-browser issues
Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete
on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.
So, if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.