• JavaScript Video Tutorials

JavaScript - Reflect.defineProperty() Method



The Reflect.defineProperty() method that allows you to define a new property or modify an existing property on an object. It is a part of the Reflect object, which offers JavaScript operations that can be intercepted. Comparing it to the Object.defineProperty() method, it offers a more powerful and flexible approach to create or modify properties.

Reflect.defineProperty() is particularly useful in situations where you need to modify properties and want to handle any errors or failures. Moreover, it permits more dynamic property definition and modification than compared to the static nature of the Object.defineProperty().

Syntax

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

Reflect.defineProperty(target, propertyKey, attributes) 

Parameters

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

  • target − The object on which you need to define or modify property.

  • propertyKey − It is the name of the property you need to define or modify.

  • attributes − An object that contains attributes property.

Return value

This method returns a Boolean values indicating whether the property was successfully defined.

Examples

Example 1

Let's look at the following example, where we are going to use the Reflect.defineProperty() and creates a empty object and assigning the value to it.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {};
         Reflect.defineProperty(x, 'Tp', {
            value: 123
         });
         document.write(x.Tp);
      </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 set enumerable and configurable to true, which indicates that the property can be deleted and iterated over the object properties.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {};
         Reflect.defineProperty(x, 'TP', {
            value: 112233,
            configurable: true,
            enumerable: true
         });
         document.write(x.TP);
      </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 writeable option to false, which indicates that it cannot be modified once it is defined.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {};
         Reflect.defineProperty(x, 'tp', {
            value: 11,
            writable: false
         });
         x.tp = 22;
         document.write(x.tp);
      </script>
   </body>
</html>

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

Example 4

Following is example, where we are going to use the Reflect.defineProperty() and modify the existing property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            prop: 1
         };
         Reflect.defineProperty(x, 'tp', {
            value: 22
         });
         document.write(x.tp);
      </script>
   </body>
</html>

On executing the above script, the output window will pop up, displaying the number on the webpage.

Advertisements