Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.
Der delete
Operator entfernt eine Property von einem Objekt.
Syntax
delete Ausdruck
wobei der Ausdruck eine Referenz auf eine Property ergeben sollte, z. B.:
delete object.property delete object['property']
Parameter
object
- Der Name eines Objekts oder ein Ausdruck der ein Objekt ergibt.
property
- Die zu löschende Property.
Rückgabewert
Throws in strict mode if the property is an own non-configurable property (returns false
in non-strict). Returns true
in all other cases.
Beschreibung
Anders als es allgemein üblich ist, hat der delete
operator nichts mit dem Freigeben des Speichers zu tun (es findet lediglich eine dereferenzierung statt. Erfahre mehr unter memory management).
War der delete
operator erfolgreich wird die Property komplett vom Objekt entfernt. Wie auch immer, sollte eine Property mit dem selben Namen in der prototype-Kette existieren, wird diese Property vererbt.
delete
ist nur effektiv an den Propertys eines Objektes, es hat kein Effekt auf Variablen oder Funktionen.
While sometimes mis-characterized as global variables, assignments that don't specify an object (e.g. x = 5
) are actually property assignments on the global object.
delete
can't remove certain properties of predefined objects (like Object, Array, Math etc). These are described in ECMAScript 5 and later as non-configurable.
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; }
Beispiele
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 }
Spezifikationen
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Die Definition von 'The delete Operator' in dieser Spezifikation. |
Standard | |
ECMAScript 5.1 (ECMA-262) Die Definition von 'The delete Operator' in dieser Spezifikation. |
Standard | |
ECMAScript 1st Edition (ECMA-262) Die Definition von 'The delete Operator' in dieser Spezifikation. |
Standard | Initial definition. Implemented in JavaScript 1.2. |
Browser Kompatibilität
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Ja) | (Ja) | (Ja) | (Ja) | (Ja) |
Temporal dead zone | ? | 36 (36) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Ja) | (Ja) | (Ja) | (Ja) | (Ja) | (Ja) |
Temporal dead zone | ? | ? | 36.0 (36) | ? | ? | ? |
Cross-browser notes
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.