Introduction to deleting Object property in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.