Attribute deletion: the delete command

The delete command is used to delete an attribute of an object.

var obj = { p: 1 };
Object.keys(obj) // ["p"]

delete obj.p // true
obj.p // undefined
Object.keys(obj) / / []
Copy the code

Note that if you delete a nonexistent property, delete does not report an error and returns true.

var obj = {};
delete obj.p // true
Copy the code

The delete command returns false in only one case, if the property exists and cannot be deleted.

var obj = Object.defineProperty({}, 'p', {
  value: 123.configurable: false
});

obj.p / / 123
delete obj.p // false
Copy the code

The delete command can only delete attributes of the object itself, but cannot delete inherited attributes

var obj = {};
delete obj.toString // true
obj.toString // function toString() { [native code] }
Copy the code

Original link:Attribute deletion