• JavaScript Video Tutorials

JavaScript - Reflect.setPrototypeOf() Method



The Reflect.setPrototypeOf() method allows you to set the prototype of an object. It is a part of the Reflect API, which offers a number of static methods for performing meta-programming tasks, such as property manipulation and interception of property operations.

If you want to modify the prototype of an existing object, you can use the Reflect.setPrototypeOf() method. This can be useful in situations where you want to create an object that inherits from a specific prototype or when you want to extend or modify the behavior of the object.

Syntax

Following is the syntax of JavaScript Reflect.setPrototypeOf() method −

Reflect.setPrototypeOf(target, prototype)

Parameters

This method accepts two parameters. The same are described below −

  • target − The object on which to set the property.

  • prototype − It holds the object new prototype.It can be any object or null.

Return value

This method returns the Boolean value indicating whether the prototype was successfully set or not.

Examples

Example 1

Let's look at the following example, where we are going to set the prototype of an object.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {};
         const y = {
            a: 10
         };
         Reflect.setPrototypeOf(x, y);
         document.write(x.a);
      </script>
   </body>
</html>

If we execute the above program, it will displays a number on the webpage.

Example 2

Consider another scenario where we are going to change the prototype of an existing object.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            a: 22
         };
         const y = {
            b: 123
         };
         Reflect.setPrototypeOf(x, y);
         document.write(x.b);
      </script>
   </body>
</html>

On executing the above script, it will displays a number on the webpage.

Example 3

In the following example, we are going to set the prototype to null.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {};
         Reflect.setPrototypeOf(x, null);
         document.write(Object.getPrototypeOf(x));
      </script>
   </body>
</html>

When we execute the above script, the output window will pop up, displaying the text on the webpage.

Example 4

Following is the example, where we are going to check if an object inherits from a prototype.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            a: 11
         };
         let y = {};
         Reflect.setPrototypeOf(y, x);
         document.write(Reflect.getPrototypeOf(y) === x);
      </script>
   </body>
</html>

On running the above script, it will generate an output consisting of the text displayed on the webpage.

Advertisements