Metoda JavaScript Object.isPrototypeOf () kontroluje, zda objekt existuje v řetězci prototypu jiného objektu.
Syntaxe isPrototypeOf()
metody je:
prototypeObj.isPrototypeOf(object)
Tady prototypeObj
je objekt.
isPrototypeOf () parametry
isPrototypeOf()
Metoda bere v:
- objekt - Objekt, jehož prototypový řetězec bude prohledán.
Vrátit hodnotu z isPrototypeOf ()
- Vrátí
Boolean
indikaci, zda volající objekt leží v řetězci prototypu zadaného objektu.
Poznámka: isPrototypeOf()
liší se od instanceof
operátora tím, že kontroluje object
prototypový řetěz proti prototypeObj
ne prototypeObj.prototype
.
Příklad: Použití 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
Výstup
true true true true
Doporučené čtení: Javascript Object setPrototypeOf ()