Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Object.setPrototypeOf() Method

  • Last Updated : 27 Sep, 2021

The Object.setPrototypeOf() method in JavaScript is standard built-in objects which sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null.
Syntax: 
 

Object.setPrototypeOf(obj, prototype)

Parameters: This method accept two parameters as mentioned above and described below: 
 

  • obj:This parameter is the object which is going to have its prototype set.
  • Prototype:This parameter is the object’s new prototype. It can be an object or null object.

Return value: This method returns the specified object.
Below examples illustrate the Object.setPrototypeOf() Method in JavaScript:
Example 1: 
 

javascript




let geek1 = { 
  prop1() { 
    return 'Object.isExtensible()'
  },
  prop2() { 
    return 'JavaScript '
  }
let geek2  = { 
  prop3() { 
    return 'Geeksforgeeks'
  
 
Object.setPrototypeOf(geek2, geek1); 
   
console.dir(geek2);
console.log(geek2.prop3());
console.log(geek2.prop2());
console.log(geek2.prop1());

Output: 
 

"Geeksforgeeks"
"JavaScript "
"Object.isExtensible()"

Example 2: 
 

javascript




var geeks1 = { 
   prop1() { 
     console.log(this.name + ' is Best platform'); 
   },
   prop2() { 
     console.log(this.name + ' provide jobs opportunity'); 
   }
}; 
   
class geeks2 { 
   constructor(name) { 
   this.name = name; 
  
   
Object.setPrototypeOf(geeks2.prototype, geeks1);  
var result = new geeks2('GeeksforGeeks'); 
result.prop1();
result.prop2();

Output: 
 

"GeeksforGeeks is Best platform"
"GeeksforGeeks provide jobs opportunity"

Supported Browsers: The browsers supported by Object.setPrototypeOf() method are listed below: 
 

  • Google Chrome 34 and above
  • Edge 12 and above
  • Firefox 31 and above
  • Internet Explorer 11 and above
  • Opera 21 and above
  • Safari 9 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!