Objet Javascript.isPrototypeOf ()

La méthode JavaScript Object.isPrototypeOf () vérifie si un objet existe dans la chaîne de prototypes d'un autre objet.

La syntaxe de la isPrototypeOf()méthode est:

 prototypeObj.isPrototypeOf(object)

Voici prototypeObjun objet.

Paramètres isPrototypeOf ()

La isPrototypeOf()méthode prend en compte:

  • object - L'objet dont la chaîne prototype sera recherchée.

Valeur renvoyée par isPrototypeOf ()

  • Renvoie un Booleanindiquant si l'objet appelant se trouve dans la chaîne de prototypes de l'objet spécifié.

Remarque: isPrototypeOf() diffère de l' instanceofopérateur car il vérifie la objectchaîne prototype par rapport à prototypeObjnon prototypeObj.prototype.

Exemple: utilisation d'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

Production

 vrai vrai vrai vrai

Lecture recommandée: Javascript Object setPrototypeOf ()

Articles intéressants...