Use _javascript techniques in the For In loop and hasOwnProperty in JavaScript

Source: Internet
Author: User
Tags hasownproperty
In contrast to the in operator, for-in loops through the prototype chain when iterating over the object's properties, for-in does not read an enumerable property, such as the length property of the group. Summary when you detect whether an object owns a property, hasOwnProperty is the only way to accomplish this task, and it is recommended to add hasOwnProperty for the in loop, which can effectively avoid errors caused by extending the local prototype.

In contrast to the in operator, for-in loops through the prototype chain when iterating over the object's properties, for-in does not read an enumerable property, such as the length property of the group.

Copy Code code as follows:

Extended Object.prototype
Object.prototype.bar = 1;
var foo = {Moo:2};
for (var i in Foo) {
Console.log (i); Output Bar and Moo
}


It is not possible to change the behavior of the For In loop, which can be done using the Object.prototype hasOwnProperty method when filtering certain properties in the loop.

Tip: Because the for in loop always traverses the entire prototype chain, it is less efficient when traversing multiple inherited objects.

Using hasOwnProperty for filtering

Copy Code code as follows:

The Foo object that is still for the previous example
for (var i in Foo) {
if (Foo.hasownproperty (i)) {
Console.log (i);
}
}


In the example, because the hasownproperty is used, the final output is moo, and if the hasOwnProperty is omitted, the code outputs the unintended result because the local prototype (such as Object.prototype) has been extended.

The prototype framework is a class library that expands JavaScript primitives and is widely used, and its drawbacks are obvious, and when the framework is introduced, if you don't use hasownproperty for filtering, the output is guaranteed to be not what you want.

Best practices

It is recommended that you always use hasOwnProperty to judge in the for, and no one can guarantee that the code environment that is running is contaminated.

hasOwnProperty
In order to check whether an object has a custom attribute that is not on the prototype chain, it is necessary to use the hasOwnProperty method, and any object has that method, which inherits from Object.prototype.

Tip: We cannot fully detect whether a property is undefined, because the attribute may exist, but its value is undefined. hasOwnProperty is the only method in JavaScript that can handle object attributes without traversing the prototype chain.

Copy Code code as follows:

Extended Object.prototype
Object.prototype.bar = 1;
var foo = {goo:undefined};

Foo.bar; 1
' Bar ' in Foo; True

Foo.hasownproperty (' Bar '); False
Foo.hasownproperty (' goo '); True


Only hasOwnProperty gives the correct expected results, which is necessary when traversing the properties of an object, and there is no other way to exclude attributes that are defined on the object's prototype chain.

hasOwnProperty as attributes

Javascript does not protect hasownproperty as a keyword or reserved word, so if an object has an attribute of the same name, it is necessary to use the extended hasownproperty to get the correct result.

Copy Code code as follows:

var foo = {
Hasownproperty:function () {
return false;
},
Bar: ' Here is Dragons '
};
Foo.hasownproperty (' Bar '); Always return False
Use another hasownproperty and set this to Foo to call it
{}.hasownproperty.call (foo, ' Bar '); True


Summary
When detecting whether an object owns a property, hasOwnProperty is the only way to accomplish this task, and it is recommended to add hasOwnProperty for the in loop, which can effectively avoid errors caused by extending the local prototype.

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.