How to check the existence of Object property in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
Tags hasownproperty
This article describes how to check the existence of Object property in JavaScript. This article describes four methods to check whether an object o has propertyx. For more information, see in JavaScript, you can use four methods to check whether an object o has property x:

1. "x" in o. The in operator can be used to check whether the property x exists in object o. X can be an object's Own (Own Property) or inherited from the prototype object. x can be an enumerable property or a non-enumerable property.

2. o. x. You can access the o. x statement and determine whether the result is undefined to check whether x exists. Its scope is the same as that of the in operator. Unlike the in operator, if the value of a property x is explicitly declared as undefined in object o, the "x" in o operation result will be true, while the o. the x result is undefined.

3. hasOwnProperty (). O. hasOwnProperty ("x") operations are used to determine whether the o object itself has x property. The property inherited from the prototype object will not be considered. The hasOwnProperty () operation checks both the property of enumerable and the property of non-enumerable.

4. propertyIsEnumerable (). The o. propertyIsEnumerable ("x") operation only checks the enumerable property of the o object. This operation is a subset of hasOwnProperty.

The above information is summarized as follows:

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.