Javascript Object.isPrototypeOf ()

Metode JavaScript Object.isPrototypeOf () memeriksa apakah suatu objek ada dalam rantai prototipe objek lain.

Sintaks dari isPrototypeOf()metode ini adalah:

 prototypeObj.isPrototypeOf(object)

Di sini, prototypeObjadalah sebuah objek.

Parameter isPrototypeOf ()

The isPrototypeOf()Metode mengambil di:

  • object - Objek yang rantai prototipe-nya akan dicari.

Nilai kembali dari isPrototypeOf ()

  • Mengembalikan yang Booleanmenunjukkan apakah objek pemanggil terletak pada rantai prototipe dari objek yang ditentukan.

Catatan: isPrototypeOf() berbeda dengan instanceofoperator karena memeriksa objectrantai prototipe dengan prototypeObjtidak prototypeObj.prototype.

Contoh: Menggunakan 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

Keluaran

 benar benar benar benar

Bacaan yang Disarankan: Javascript Object setPrototypeOf ()

Artikel yang menarik...