Javascript Object.setPrototypeOf ()

JavaScript Object.setPrototypeOf () yöntemi, belirtilen bir nesnenin prototipini başka bir nesneye veya boş olarak ayarlar.

setPrototypeOf()Yöntemin sözdizimi şöyledir:

 Object.setPrototypeOf(obj, prototype)

setPrototypeOf()Yöntem, statik bir yöntemle olmak kullanılarak denir Objectsınıf adını.

setPrototypeOf () Parametreler

setPrototypeOf()Yöntem alır:

  • obj - Prototipi ayarlanacak nesne.
  • prototype - Nesnenin yeni prototipi (bir nesne veya boş).

SetPrototypeOf () 'dan dönen değer

  • Belirtilen nesneyi döndürür.

Not:((Prototype)) Bir nesnenin değiştirilmesi şu anda her tarayıcıda ve JavaScript motorunda çok yavaş bir işlemdir.

Örnek 1: Object.setPrototypeOf () kullanma

 let Animal = ( makeSound() ( console.log(`$(this.name), $(this.sound)!`); ), ); // defining new Dog object function Dog(name) ( this.name = name; this.sound = "bark"; // setting prototype to Animal Object.setPrototypeOf(this, Animal); ) dog1 = new Dog("Marcus"); dog1.makeSound(); // Marcus, bark!

Çıktı

 Marcus, havla!

Örnek 2: Object.setPrototypeOf () kullanma

 let Animal = ( makeSound() ( console.log(`$(this.name), $(this.sound)!`); ), ); // defining object class Dog ( constructor(name, age) ( this.name = name; this.sound = "bark"; ) introduce() ( console.log(`I'm $(this.name). I am $(this.age) years old.`); ) ) // Here Dog.prototype is passed as it is an object, while Dog is not an object Object.setPrototypeOf(Dog.prototype, Animal); dog1 = new Dog("Marcus", 3); console.log(dog1); dog1.makeSound(); // Marcus, bark!

Çıktı

 name: "Marcus" ses: "bark" __proto__: yapıcı: class Köpek tanıtımı: ƒ tanıtmak () __proto__: makeSound: ƒ makeSound () __proto__: Object Marcus, havla!

Önerilen Okuma: Javascript Object isPrototypeOf ()

Ilginç makaleler...