3 Ways to traverse the property of an object in JavaScript introduce _javascript techniques

Source: Internet
Author: User

In JavaScript, you can traverse the property of an object in three ways:

1.for/in. You can use the For/in statement to traverse the property of the object itself and the property that inherits from the prototype object, and only the property of the enumerable is traversed to the Own.

2.object.keys (). You can pass an object as a parameter to Object.keys (), and the Object.keys () statement returns an array of all the property name strings. The Object.keys () statement returns only the property of the object itself (Own property) and the enumerable property. The statement is valid only in the ECMAScript 5 standard.

3.object.getownpropertynames (). You can pass an object as a parameter to Object.getownpropertynames (), and, like Object.keys (), the statement returns an array of all the property name strings. Unlike Object.keys (), the Object.getownpropertynames () statement returns the property (the Own property) of all objects, regardless of whether they are enumerable. The statement is valid only in the ECMAScript 5 standard.

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

Experiment:


Copy Code code as follows:

var o = {x:1, y:2};
var a = Object.create (o);
A.Z = 3;

For (p in a) {
Console.log (P);
}//z x y
Console.log (Object.keys (a));//["Z"]
Console.log (Object.getownpropertynames (a));//["Z"]

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.