Both properties are provided by Object.prototype: Object.prototype.hasOwnProperty() and Object.prototype.isPropertyOf()
First explain the hasOwnProperty() method and use. Explain the isPropertyOf() method and use
Understand these at least you must understand the prototype chain
1. Object.prototype.hasOwnProperty()
Overview
hasOwnProperty() method is used to determine whether an object contains a specified property
grammar
obj.hasOwnProperty("property name");// Whether the instance obj contains the properties in parentheses, if it is true, otherwise it is false
describe
All objects that inherit Object.prototype will be inherited from the prototype chain to the hasOwnProperty method. This method detects whether an object contains a specific property. Unlike in, this method will ignore properties inherited from the prototype chain.
Example
1. Use hasOwnProperty() method to determine whether an object contains a specific property
The following example detects whether object o contains its own attribute prop:
var o =new Object();o.prop="exists";function change(){o.newprop=o.prop;delete o.prop;}o.hasOwnProperty("prop")//truechange()//Delete the prop property of o.hasOwnProperty("prop")//false//After deletion, use hasOwnProperty() to determine whether it exists, and the return does not exist2. The difference between your own attributes and inherited attributes
The following columns demonstrate the difference between the hasOwnProperty() method treats its own properties and inherited properties.
var o =new Object();o.prop="exists";o.hasOwnProperty("prop");//true Its own attributes o.hasOwnProperty("toString");//false Methods inherited from the Object prototype o.hasOwnProperty("hasOwnProperty");//false Methods inherited from the Object prototype3. After modifying the prototype chain, the pointing example of hasOwnProperty() is
The following columns demonstrate the difference between the hasOwnProperty() method in the inheritance properties after modifying the prototype chain.
var o={name:'jim'};function Person(){this.age=19;}Person.prototype=o;//Modify Person's prototype to point to p.hasOwnProperty("name");//false Cannot judge the inherited name attribute p.hasOwnProperty("age");//true;4. Use hasOwnProperty() to traverse the properties of an object itself
The following columns demonstrate how to ignore inherited attributes while traversing an object and obtain its own attributes.
Note ・Forin will traverse the enumerable properties in object inheritance
var o={gender:'ma'}function Person(){this.name="Zhang San";this.age=19;}Person.prototype=o;var p =new Person();for(var k in p){if(p.hasOwnProperty(k)){console.log("Original properties: "+k);// name ,age}else{console.log("Inheriting properties elsewhere: "+k);// gender}}5. HasOwnProperty method may be overwritten
If an object has its own hasOwnProperty() method, the hasOwnProperty() method on the prototype chain will be overwritten
var o={gender:'male',hasOwnProperty:function(){return false;}}o.hasOwnProperty("gender");//Return false if you don't write anything//Solution method, use call method({}).hasOwnProperty.call(o,'gender');//trueObject.prototype.hasOwnProperty.call(o,'gender');//true2. Object.prototype.isPrototypeOf()
Overview
isPrototypeOf() method tests whether an object exists on the prototype chain of another object
grammar
//Is object1 the prototype of Object2? That is to say, Object2 is the prototype of Object1. If it is, it returns true, otherwise falseobject1.isPrototypeOf(Object2);
describe
isPrototypeOf() method allows you to check whether an object exists on the prototype chain of another object.
Example
1. Use isPrototypeOf() to check whether an object exists on the prototype of another object
var o={};function Person(){};var p1 =new Person();//Inherited from the original prototype, but now it is impossible to access Person.prototype=o;var p2 =new Person();//Inherited from oconsole.log(o.isPrototypeOf(p1));//false is o a prototype of p1 console.log(o.isPrototypeof(p2));//true is o a prototype of p22. Use isPropertyOf() to check whether an object exists on the prototype chain of another object
var o={};function Person(){};var p1 =new Person();//Inherited from the original prototype, but now it is impossible to access Person.prototype=o;var p2 =new Person();//Inherited from oconsole.log(o.isPrototypeOf(p1));//false o is the prototype of p1 console.log(o.isPrototypeof(p2));//true o is the prototype of p2 console.log(Object.prototype.isPrototypeOf(p1));//trueconsole.log(Object.prototype.isPrototypeOf(p2));//trueThe prototype chain structure of p1 is p1=>The original Person.prototype=>Object.prototype=>null
The prototype chain structure of p2 is p2=>o =>Object.prototype=>null
Both p1 and p2 have Object.prototype so they are both on the prototype chain of Object.Prototype
3. Summary
1.hasOwnProperty: is used to determine whether an object has a property or object that you gave the name. However, it should be noted that this method cannot check whether the object's prototype chain has this property, which must be a member of the object itself.
2. isPrototypeOf is used to determine whether the object to check whether its prototype chain exists in the specified object instance. If it is, it returns true, otherwise it returns false.
The above is a detailed explanation of the hasOwnProperty() and isPrototypeOf() attribute examples in JS introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!