ES6 - Object.setPrototypeOf

Con l'aiuto di questa funzione, possiamo impostare il prototipo di un oggetto specificato su un altro oggetto o null.

Sintassi

In questa sintassi, obj è l'oggetto che deve avere il suo prototipo impostato e prototype è il nuovo prototipo dell'oggetto (un oggetto o null).

Object.setPrototypeOf(obj, prototype)

Esempio

<script>
   let emp = {name:'A',location:'Mumbai',basic:5000}
   let mgr = {name:'B'}
   console.log(emp.__proto__ == Object.prototype)
   console.log(mgr.__proto__ == Object.prototype)
   console.log(mgr.__proto__ ===emp.__proto__)
   Object.setPrototypeOf(mgr,emp)
   console.log(mgr.__proto__ == Object.prototype) //false
   console.log(mgr.__proto__ === emp)
   console.log(mgr.location,mgr.basic)

</script>

L'output del codice sopra sarà come indicato di seguito -

true
true
true
false
true
Mumbai 5000