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