The existence method of checking object property in JavaScript introduction of _javascript techniques

Source: Internet
Author: User
Tags hasownproperty

In JavaScript, there are four ways to check whether an object o owns property x:

1. "X" in O. The in operator can be used to check if there is an X in Object o. X can be the object itself (Own property), or it can be inherited from a prototype object; X can be a enumerable property, or it can be a enumerable property.

2.o.x. The o.x statement can be accessed to determine whether the result is undefined to confirm that x exists and that the scope of the action is the same as in the in operator. Unlike the In operator: if you explicitly declare the value of a property x to be undefined in object o, the "x" in O operation results will be true, and the o.x result is undefined.

3.hasOwnProperty (). The O.hasownproperty ("x") operation is used to determine whether the O object itself owns X Property,o the property that inherits from the prototype object will not be considered. The hasOwnProperty () operation checks both the enumerable property and the non enumerable property.

4.propertyIsEnumerable (). The O.propertyisenumerable ("x") operation checks only the enumerable property owned by the O object itself, which is a subset of hasOwnProperty ().

Combined with the above information, summarized in the following diagram:

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.