Metoda JavaScript Object.isPrototypeOf () verifică dacă un obiect există în lanțul de prototip al unui alt obiect.
Sintaxa isPrototypeOf()
metodei este:
prototypeObj.isPrototypeOf(object)
Iată prototypeObj
un obiect.
isPrototypeOf () Parametri
isPrototypeOf()
Metoda ia în:
- obiect - Obiectul al cărui lanț prototip va fi căutat.
Valoare returnată de la isPrototypeOf ()
- Returnează o
Boolean
indicație dacă obiectul apelant se află în lanțul prototip al obiectului specificat.
Notă: isPrototypeOf()
diferă de instanceof
operator, deoarece verifică object
lanțul prototip împotriva prototypeObj
nu prototypeObj.prototype
.
Exemplu: Utilizarea Object.isPrototypeOf ()
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
Ieșire
adevărat adevărat adevărat adevărat
Lectură recomandată: Javascript Object setPrototypeOf ()