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 has to be set.
prototype: It represents the object’s new prototype.

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

Note: It throws TypeError if the 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>

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>