Objek Javascript.isPrototypeOf ()

Kaedah JavaScript Object.isPrototypeOf () memeriksa apakah objek wujud dalam rantai prototaip objek lain.

Sintaks isPrototypeOf()kaedahnya adalah:

 prototypeObj.isPrototypeOf(object)

Di sini, prototypeObjadalah objek.

Parameter isPrototypeOf ()

The isPrototypeOf()Cara mengambil masa dalam:

  • objek - Objek yang rantai prototaipnya akan dicari.

Nilai pulangan dari isPrototypeOf ()

  • Mengembalikan yang Booleanmenunjukkan sama ada objek memanggil terletak pada rantai prototaip objek yang ditentukan.

Catatan: isPrototypeOf() berbeza dengan instanceofpengendali kerana memeriksa objectrantai prototaip terhadap 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

Pengeluaran

 benar benar benar benar

Bacaan yang Disyorkan: Set Objek JavascriptPrototypeOf ()

Artikel menarik...