Reflect.setPrototypeOf() JavaScript

The JavaScript Reflect.setPrototypeOf() method sets the prototype of an object to another object. It is similar to Object.setPrototypeOf().

Syntax:

Reflect.setPrototypeOf(target, prototype)

Parameters:
target: It represents the object whose prototype have to be set.
prototype: It represents the object’s new prototype.

Return:
It returns true if the object’s new prototype set successfully otherwise returns false.

Note: It throws TypeError, if target is not an Object or null.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>  
var Jewel = {
gems() {
document.write(this.a + ' is very precious.');}};
class stone {
constructor(a) {
this.a = a;}}
Reflect.setPrototypeOf(stone.prototype, Jewel);
var d = new stone('DIAMOND');
d.gems();  
</script>
</body>
</html>

Output:

DIAMOND is very precious.

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
 
const object1 = {};
 
document.write(Reflect.setPrototypeOf(object1, Object.prototype));
document.write("</br>");
document.write(Reflect.setPrototypeOf(object1, null));
document.write("</br>");
 
const object2 = {};
 
document.write(Reflect.setPrototypeOf(Object.freeze(object2), null));
</script>
</body>
</html>

Output:

true
true
false
Please Share