High-performance JavaScript traversal object properties

Source: Internet
Author: User
Tags hasownproperty

In object-oriented development, it is often the case to check object properties and Traverse object properties. JavaScript does not contain traditional class inheritance models such as Java and C, but instead uses the prototype prototype model.
JavaScript prototype chain inheritanceIn the property lookup process

When a property is found for an object, JavaScript traverses the prototype chain up until it finds a property of the given name.
To find the top of the prototype chain-that is, Object.prototype-however, the specified property is still not found and the undefined is returned.

Use for inLoops can traverse all properties of an object, including the object's prototype chainThe properties in, such as:
var a = {
A:1,
B:2
};

var B = Object.create (a);

B.C = 3;

var C = object.create (b);

C.D = 4;

for (var key in C) {
Console.log (C[key])
}

Will print in the console 4 3 1 2 sequentially


Note that the order of printing in the console is 4 3 1 2 instead of 1 2 3 4, which explains that the process of locating a property when the prototype chain inherits is to find its own property, which is searched in the prototype chain when its own property does not exist.

Sometimes we do not want this result, we just want to get the properties of the object itself (not including the properties on the object prototype chain), and each traversal to find the object's prototype chain, which will cause a performance burden.

hasownproperty function

hasOwnPropertyThe function can be used to check whether the object itself contains a property, the return value is a Boolean value, and does not look up the object's prototype chain when the property does not exist.

Use an example to see hasOwnPropertyAnd for inThe difference:

if (var "a" in C) {
Console.log (c["a"])//attribute A is a property on the prototype chain, Output 1
}

if (C.hasownproperty ("a")) {
Console.log (c["a"])//attribute A is not a property of itself and does not perform this step
}

getownpropertynames function

GetownpropertynamesThe function can get all of the object's own properties, the return value is an array of the object's own property name, and does not look up the object prototype chain.

Such as:

Console.log (Object.getownpropertynames (c))//Output ["D"]

GetownpropertynamesThe function iterates through all of the object's own properties, for example:

(Fucntion () {
var propertys = Object.getownpropertynames (c);
var len = propertys.length;

for (var i = 0; i < len; i++) {
var key = Propertys[i];
Console.log (C[key])//Output 4
}
})();

More exciting content sharing, Dabigatran 434623999

High-performance JavaScript traversal object properties

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.