The difference between in and hasOwnProperty JavaScript

Source: Internet
Author: User

Each time the code reads a property of an object, the search is performed once, and the target is a property with the given name. The search begins first from the object instance itself. If a property with the given name is found in the instance, the value of the property is returned, and if it is not found, the prototype object that the pointer points to is searched, and the property with the given name is looked up in the prototype object. If this property is found in the prototype object, the value of the property is returned. Although the values saved in the prototype can be accessed through an object instance, the values in the prototype cannot be overridden through an object instance. If you add a property in the instance with the same name as a property in the prototype, that property masks that property in the prototype. Adding a property with the same name only prevents us from accessing that property in the prototype, but does not modify that property. Even if you set this property to NULL, this property is set only in the instance, not the link to the prototype.

The In operator returns true as long as the property is accessible through the object. hasOwnProperty () returns true only if the property exists in the instance.

Using the delete operator allows you to completely remove the instance properties, allowing us to consolidate the re-access properties in the prototype.

1 functionPerson () {2 }3Person.prototype.name = "Nicholas";4Person.prototype.age = 29;5Person.prototype.job = "Software Engineer";6Person.prototype.sayName =function(){7Alert This. Name);8 };9 Ten varPerson1 =NewPerson (); One varPerson2 =NewPerson (); A  -Alert (Person1.hasownproperty ("name"));//false -Alert ("Name"inchPerson1);//true the  -Person1.name = "Greg"; -alert (person1.name);//"Greg" -Alert (Person1.hasownproperty ("name"));//true +Alert ("Name"inchPerson1);//true -  + DeletePerson1.name; Aalert (person1.name);//"Nicholas" atAlert (Person1.hasownproperty ("name"));//false -Alert ("Name"inchPerson1);//true

The difference between in and hasOwnProperty JavaScript

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.