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, prototypeObj
adalah sebuah objek.
Parameter isPrototypeOf ()
The isPrototypeOf()
Metode mengambil di:
- object - Objek yang rantai prototipe-nya akan dicari.
Nilai kembali dari isPrototypeOf ()
- Mengembalikan yang
Boolean
menunjukkan apakah objek pemanggil terletak pada rantai prototipe dari objek yang ditentukan.
Catatan: isPrototypeOf()
berbeda dengan instanceof
operator karena memeriksa object
rantai prototipe dengan prototypeObj
tidak 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 ()