JavaScript, O detection properties

Source: Internet
Author: User
Tags hasownproperty

JavaScript objects can be thought of as a collection of attributes, and we often detect the relationships in the members of the collection and determine whether the property is in this object, so we can pass in, hasOwnProperty (), propertyisenumerable ()

and other methods. These methods are described separately below:


One, in detects whether the property belongs to the object.

The on left side is the property name, the right is the object, and returns True if the object's inherited property or its own property includes this property:

var o={x:1};

"X" in O//true x is the property of O

"ToString" in O//True o inherits the ToString property


The hasOwnProperty () method is used to detect whether a given name is a free property of an object. For an inherited property return false example

var o={x:1};

O.hasownproperty ("x")//true:o Have a free attribute X

O.hasownproperty ("toString"); False ToString is an inherited property:


Third, propertyisenumerable () is a hasownproperty () enhanced version, not only to verify the free property of the object, but also to verify that the property, whether Enumerable. As follows

var o={x:1};

O.propertyisenumerable (x)//True, X is the free property of O and can be enumerated

Object.property.propertyIsEnumerable ("tostirng")//false non-enumerable

You

In addition to using the in operator, the more simple way to use "! = = "Determines whether the property is undefined, as shown in the following code:

var o={x:1};

o.x!==undefined//true O has attribute x

o.tostring!==undefined//true O has inheritance property in ToString

However, there is a situation where you can use the in operator only, not the method above

As follows:

var o={x:undefined};

O.x!==undefined//false Property exists but has a value of undefined

"X" in O//true exists attribute X


JavaScript, O detection properties

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.