JavaScript Secret Garden-Hasownproperty,functions,this__java

Source: Internet
Author: User
Tags hasownproperty
hasOwnProperty

To determine whether an object contains custom attributes rather than attributes on the prototype chain, we need to use the hasOwnProperty method that inherits from Object.prototype.

Note: It is not sufficient to determine whether a property is undefined or not. Because a property may indeed exist, its value is set to undefined.

hasOwnProperty is the only method in JavaScript that handles attributes but does not need to look up a prototype chain.

1 2 3 4 5 6 7 8 9 Modify 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 can give the correct and expected results, which is useful when traversing the properties of an object. There is no other way to exclude attributes from the prototype chain, rather than to define the properties on the object itself. hasOwnProperty as attributes (hasOwnProperty as a property)

JavaScript does not protect hasOwnProperty from being illegally occupied, so if an object happens to have this attribute, you need to use an external hasownproperty function to get the correct result.

1

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.