Js hasOwnProperty() and isPrototypeOf()

These two properties are Object. The prototype provided: Object. The prototype. The hasOwnProperty () and Object. The prototype. IsPropertyOf’s explanation hasOwnProperty () () method and use. In the isPropertyOf() method and use

You have to understand at least the prototype chain

First, the Object. The prototype. HasOwnProperty ()

An overview of the

The hasOwnProperty() method is used to determine whether an object has a specified property of its own

grammar

obj.hasOwnProperty("Attribute name"); // If instance obj contains attributes in parentheses, return if it doestrue, it isfalseCopy the code

describe

All objects that inherit from Object.prototype are inherited from the prototype chain to the hasOwnProperty method, which checks if an Object contains a particular property. Unlike in, this method ignores properties inherited from the prototype chain.

The instance

1. Use the hasOwnProperty() method to determine whether an object has a specific property of its own

The following example checks whether object O contains its own property prop:

var o =new Object();
o.prop="exists";

function change(){
  o.newprop=o.prop;
  delete o.prop;
}

o.hasOwnProperty("prop") / /trueChange ()// Delete prop property of o."prop") / /falseHasOwnProperty () does not existCopy the code

2. The difference between its own attributes and inherited attributes

The following example demonstrates the difference between the hasOwnProperty() method’s treatment of its own and inherited properties.

var o =new Object();
o.prop="exists";
o.hasOwnProperty("prop"); //trueIts own property o.hasownProperty ("toString"); //falseMethod inheriting from the Object prototype o.hasownProperty ("hasOwnProperty"); //falseMethods inherited from Object prototypesCopy the code

3. Pointing example of hasOwnProperty() after modifying the prototype chain

The following example demonstrates how the hasOwnProperty() method treats inherited properties differently after modifying the stereotype chain

var o={name:'jim'};
function Person(){ this.age=19; } Person.prototype=o; // Change the prototype of Person to point to p.hasownProperty ("name"); //falseUnable to determine inherited name property p.hasownProperty ("age"); //true;Copy the code

4. Use hasOwnProperty() to iterate over an object’s own properties

The following example demonstrates how to iterate over an object, ignoring inherited properties, to get its own properties.

Pay attention to,forinEnumerable properties in object inheritance are iterated over

var o={
  gender:'male'
}
function Person(){
  this.name="Zhang";
  this.age=19;
}
Person.prototype=o;
var p =new Person();
for(var k in p){
  if(p.hasOwnProperty(k)){
    console.log("Self attribute:"+k); // name ,age }else{
    console.log("Inherit properties from elsewhere:"+k); // gender } }Copy the code

5. HasOwnProperty methods may be overwritten

If an object has its own hasOwnProperty() method, the hasOwnProperty() method on the prototype chain will be overridden

var o={
  gender:'male',
  hasOwnProperty:function() {return false;
  }
}

o.hasOwnProperty("gender"); // Return no matter what is writtenfalse// Use the call method ({}).hasownProperty.call (o,'gender'); //true
Object.prototype.hasOwnProperty.call(o,'gender'); //trueCopy the code

Second, the Object. The prototype. IsPrototypeOf ()

An overview of the

The isPrototypeOf() method tests whether an object exists on the prototype chain of another object

grammar

//object1 is the prototype of Object2, so Object2 is the prototype of object1true, otherwise,false
object1.isPrototypeOf(Object2);Copy the code

describe

The isPrototypeOf() method allows you to check if an object exists on the prototype chain of another object

The instance

1. Use isPrototypeOf() to check whether an object exists on the prototype of another object

var o={};
function Person() {}; var p1 =new Person(); Prototype =o; // Inherits from the original prototype, but is no longer accessible. var p2 =new Person(); // Inherited from o. console.log(o. isprototypeof (p1)); //falseConsole. log(o. isprototypeof (p2)); //trueO is not a prototype for P2Copy the code

2. Use isPropertyOf() to check if one object is attached to another object’s prototype chain

var o={};
function Person() {}; var p1 =new Person(); Prototype =o; // Inherits from the original prototype, but is no longer accessible. var p2 =new Person(); // Inherited from o. console.log(o. isprototypeof (p1)); //falseConsole. log(o. isprototypeof (p2)); //trueO is the prototype of the p2 console. The log (Object. The prototype. IsPrototypeOf (p1)); //trueconsole.log(Object.prototype.isPrototypeOf(p2)); //trueCopy the code

Prototype =>null Person. Prototype =>Object. Prototype =>null Person Both P1 and P2 have Object.prototype so they are on the Prototype chain of Object.prototype

Third, summary

  1. HasOwnProperty: Determines whether an object has a property or object with the name you give it. Note, however, that this method does not check whether the property is present in the prototype chain of the object, which must be a member of the object itself.
  2. IsPrototypeOf is used to determine whether the object whose prototype chain is to be checked exists in the specified object instance, returning true if it exists, and false otherwise.