• JavaScript Video Tutorials

JavaScript - Reflect.get() Method



The Reflect.get() method is a part of Reflect API, that provides a collection of static methods that are used to perform operation on objects similar to those performed by operators. This method is used to retrieve the property value of an object, similar to the dot operation or bracket notation but with additional features and flexibility.

Syntax

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

Reflect.get(target, propertyKey, receiver) 

Parameters

This method accepts three parameter. The same are described below −

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

  • propertyKey − Name of the property to get.

  • receiver − It's a optional parameter and it is the value of 'this' provided for the call to target.

Return value

This method returns the value of the property.

Examples

Example 1

Let's look at the following example, where we are going to use the Reflect.get() and retrieve the value of the property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            car: 'Maserati',
            model: 2024
         };
         const y = Reflect.get(x, 'car');
         document.write(JSON.stringify(y));
      </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 access properties using Reflect.get() defined with symbols.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = Symbol('tp');
         const y = {
            [x]: 'TutorialsPoint'
         };
         const z = Reflect.get(y, x);
         document.write(z);
      </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 access the property that doesn't exists and checking the output.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            state: 'Telangana',
            capital: 'Hyderabad'
         };
         const y = Reflect.get(x, 'population');
         document.write(y);
      </script>
   </body>
</html>

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

Advertisements