Use of the JavaScript delete attribute

Source: Internet
Author: User

Delete is an attribute of an object. For example, for an object,
Var obj = {key: 5 };
Delete obj. key is to delete the key attribute of the object. This is no problem, but it is worth noting that the prototype object of the object also has this attribute.
Copy codeThe Code is as follows:
Var A = function (){};
A. prototype. testMe = true;
Var a = new ();
// Overwrite the prototype Property
A. testMe = true;
If (a. testMe ){
// Some key code...
//....
// Delete this attribute
Delete a. testMe;
}
// Section 2 ---------------------------
// In other modules
If (a. testMe ){
// Some key code...
//....
}

The second paragraph is worth noting. Do not think that testMe IN a does not exist if it has been deleted, so. testMe is undefined, which is false. In fact, it still exists through prototype access, or true!
This is just a move.
// Appendix:
Check whether an object has a property, including the prototype chain:
If ('attrname' in obj )...
Check whether an object has a certain attribute, which belongs to the object rather than the prototype chain:
Obj. hasOwnProperty ('attrname ')

Related Article

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.