Deleting Properties
The delete operator (The delete Operator) removes a property from an object. Its
single operand should be a property access expression. Surprisingly,
delete does not operate on the
value of the property but on the property itself:
deletebook.author;// The book object now has no author property.deletebook["main title"];// Now it doesn't have "main title", either.
The delete operator only
deletes own properties, not inherited ones. (To delete an inherited
property, you must delete it from the prototype object in which it is
defined. Doing this affects every object that inherits from that
prototype.)
A delete expression evaluates
to true if the delete succeeded or
if the delete had no effect (such as deleting a nonexistent property).
delete also evaluates to true when used (meaninglessly) with an
expression that is not a property access expression:
o={x:1};// o has own property x and inherits property toStringdeleteo.x;// Delete x, and return truedeleteo.x;// Do nothing (x doesn't exist), and return truedeleteo.toString;// Do nothing (toString isn't an own property), return truedelete1;// Nonsense, but evaluates to true
delete does not remove
properties that have a configurable attribute of
false. (Though it will remove configurable properties of nonextensible objects.) Certain properties of built-in objects are nonconfigurable, as are properties of the global object created by variable declaration and function declaration. In strict mode, attempting ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access