Open In App

JavaScript Reflect setPrototypeOf() Method

Last Updated : 30 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Reflect.setPrototypeOf() method in JavaScript is used to set the prototype of a specified object to another object or to Null. This method returns the boolean value for the operation that is successful. This method is the same as the Object.setPrototypeOf() method.

Syntax:

Reflect.setPrototypeOf(obj, prototype)

Parameters:

  • Obj: This parameter holds the target object and it is used to set the prototype.
  • Prototype: This parameter holds the object’s new prototype. It can be any object or simply a null.

Return value: This method always returns a Boolean value which indicates whether setting the prototype was successfully done or not.

Exceptions: A TypeError is an exception given as the result when the target is not an object.

Below examples illustrate the Reflect.setPrototypeOf() method in JavaScript:

Example 1: In this example, we will set the prototype of an object using the Reflect.setPrototypeOf() method in JavaScript.

javascript




const object1 = {};
  
console.log(Reflect.setPrototypeOf(object1, Object.prototype));
console.log(Reflect.setPrototypeOf(object1, null));
  
const object2 = {};
console.log(Reflect.setPrototypeOf(Object.freeze(object2), null));
  
let object3 = {
    gfg() {
        return 'value';
    }
}
let obj = {
    geeks() {
        return 'answer';
    }
}
Object.setPrototypeOf(obj, object3);
console.dir(obj);
console.log(obj.geeks());
console.log(obj.gfg());


Output:

true
true
false
"answer"
"value"

Example 2: In this example, we will set the prototype of an object using the Reflect.setPrototypeOf() method in JavaScript.

javascript




console.log(Reflect.setPrototypeOf({}, Object.prototype));
  
// It can change an object's [[Prototype]] to null.
console.log(Reflect.setPrototypeOf({}, null));
  
// Returns false if target is not extensible.
console.log(Reflect.setPrototypeOf(Object.freeze({}), null));
  
// Returns false if it cause a prototype chain cycle.
let target = {}
let proto = Object.create(target)
console.log(Reflect.setPrototypeOf(target, proto));
  
let object3 = {
    gfg() {
        console.log(this.name + ' have fun.');
    }
};
class objt {
    constructor(name) {
        this.name = name;
    }
}
Reflect.setPrototypeOf(objt.prototype, object3);
  
// If you do not do this you will get
// a TypeError when you invoke speak  
let val = new objt('Geeks');
val.gfg();


Output:

true
true
false
false
"Geeks have fun."

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

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 42 and above
  • Opera 36 and above
  • Safari 10 and above

We have a complete list of Javascript Reflects methods, to check those go through the JavaScript Reflect Reference article.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads