ES6 - Object.setPrototypeOf



With the help of this function, we can set the prototype of a specified object to another object or null.

Syntax

In this syntax, obj is the object which is to have its prototype set and prototype is the object's new prototype (an object or null).

Object.setPrototypeOf(obj, prototype)

Example

<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>

The output of the above code will be as mentioned below −

true
true
true
false
true
Mumbai 5000
Advertisements