• JavaScript Video Tutorials

JavaScript - Reflect.getPrototypeOf() Method



The Reflect.getPrototypeOf() method is used to return the prototype of an object. It is similar to the Object.getPrototypeOf() method, but it's part of the Reflect object, which provides a collection of methods for performing meta-operations on objects.

The main advantages of using the Reflect.getPrototypeOf() method is its usage in conjunction with the Proxy API. It helps you to intercept and modify operations on objects, including the retrieval of prototypes. This method provides a reflection API for JavaScript operations, enhancing the introspection capabilities of the language.

Syntax

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

Reflect.getPrototypeOf(obj)

Parameters

This method accepts only one parameter. The same are described below −

  • obj − The object whose prototype you want to retrieve.

Return value

This method returns the prototype of the given object.

Examples

Example 1

Let's look at the following example, where we are going to use Reflect.getPrototypeOf() to retrieve the prototype of an empty object.

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

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

Example 2

Consider another scenario where we are going to use the object literals and create a object.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            car: 'RS6'
         };
         document.write(Reflect.getPrototypeOf(x) === Object.prototype);
      </script>
   </body>
</html>

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

Example 3

In the following example, we are going to get the prototype of an array.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = [11, 22, 33];
         const prototype = Reflect.getPrototypeOf(x);
         document.write(JSON.stringify(prototype));
      </script>
   </body>
</html>

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

Advertisements