This article mainly introduces how to delete the property of an object in JavaScript. This article mainly introduces the delete operator. If you need it, you can refer to JavaScript, you can use the delete operator to delete the property in an object:
The Code is as follows:
Var t = {a: 42, B: 26 };
Console. log (t); // Object {a = 42, B = 26}
Delete t.;
Console. log (t); // Object {B = 26}
This property deletion operation has the following limitations: the delete operator can only delete all the properties of an object and cannot delete the properties inherited from the prototype object. To delete a property in a prototype object, you must obtain the prototype object explicitly and perform the following operations on the prototype object:
The Code is as follows:
Var o = {x: 1, y: 2 };
Var a = Object. create (o );
A. z = 3;
Console. log (a); // Object {z = 3, x = 1, y = 2}
Delete a. x; // Can NOT delete inherited property
Console. log (a); // Object {z = 3, x = 1, y = 2}
Delete a. z; // Can delete own property
Console. log (a); // Object {x = 1, y = 2}
Delete a. _ proto _. x;
Console. log (a); // Object {y = 2}
If the property in the prototype object is deleted, all objects inherited from the prototype object will be affected.
For the return value of the delete operation, JavaScript follows the following rules:
1. If the delete operation is successful, true is returned.
2. If the delete operation has no effect (for example, the property to be deleted does not exist), true is returned.
3. If the property to be deleted and its retriable attribute is false, A TypeError error is reported in strict mode, and false is returned in non-strict mode.
If the delete operator is used for the property of a global object, the global object in the code can be omitted in non-strict mode:
The Code is as follows:
This. c = 42;
Delete c; // equal to delete this. c;
Note that in strict mode, the above write method will throw the SyntaxError error.