Javascript Object.isPrototypeOf ()

JavaScript Object.isPrototypeOf () -menetelmä tarkistaa, onko objektia toisen objektin prototyyppiketjussa.

Menetelmän syntaksi isPrototypeOf()on:

 prototypeObj.isPrototypeOf(object)

Tässä prototypeObjon esine.

isPrototypeOf () -parametrit

isPrototypeOf()Menetelmä vie:

  • object - objekti, jonka prototyyppiketjua etsitään.

Palautusarvo kohteesta isPrototypeOf ()

  • Palauttaa Booleanosoituksen siitä, onko kutsuva objekti määritetyn objektin prototyyppiketjussa.

Huomautus: isPrototypeOf() eroaa instanceoftoimijan se tarkistaa objectprototyyppi ketjua vastaan prototypeObjole prototypeObj.prototype.

Esimerkki: Object.isPrototypeOf (): n käyttö

 let obj = new Object(); console.log(Object.prototype.isPrototypeOf(obj)); // true console.log(Function.prototype.isPrototypeOf(obj.toString)); // true console.log(Array.prototype.isPrototypeOf((2, 4, 8))); // true // define object let Animal = ( makeSound() ( console.log(`$(this.name), $(this.sound)!`); ), ); // new object function Dog(name) ( this.name = name; this.sound = "bark"; // setting prototype using setPrototypeOf() Object.setPrototypeOf(this, Animal); ) dog1 = new Dog("Marcus"); console.log(Animal.isPrototypeOf(dog1)); // true

Tuotos

 true true true true

Suositeltava lukeminen: Javascript Object setPrototypeOf ()

Mielenkiintoisia artikkeleita...