JavaScript Object Attributes

Source: Internet
Author: User

Generally,."Operator to access the property value of an object. The value on the left of the vertex operator is the object for accessing its attributes. The value on the right of the vertex operator should be the attribute name. The properties of an object are similar to those of a variable. You can store the value or read the value from it.

① Attribute Enumeration

For/inLoop provides a way to traverse object attributes. The following code shows a function that can be used to list all attribute names of an object:

Function displaypropertynames (OBJ) {var names = ""; for (VAR name in OBJ) Names + = Name + "\ n"; alert (names );}

Note that the for/in attributes are not listed in a specific order. Although it can enumerate all user-defined attributes, it cannot enumerate some predefined attributes or methods.

② Check the existence of attributes

InOperators can be used to test the existence of an attribute. For example

If ("X" in O) o. x = 1;

In fact, the above statements are rare. If the query does not have an attribute, the undefined value is returned. Therefore, the above Code is usually written in this way.

If (O. X! = Undefined) o. x = 1;

Note that an attribute may also exist but is still undefined. For example, o. x = undefined; Attribute x already exists but has no value.

③ Delete attributes

AvailableDeleteOperator to delete the attributes of an object:

Delete book. Chapter2;

Deleting a property actually removes the property from the object. After deletion, the for/in loop does not enumerate this attribute, and the in operator does not detect this attribute.


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.