• JavaScript Video Tutorials

JavaScript - Reflect.preventExtensions() Method



The Reflect.preventExtensions() method is used to prevent new properties from being added to an object. It works similarly to the Object.preventExtensions() method, but it is a part of the Reflect object, which offers a more consistent and standardized way to perform operations on objects.

If you want to make sure that an object cannot be changed after it has been created, you can use the Reflect.preventExtensions() method. This can be useful in situations where you want to ensure that certain properties of an object remain constant, or when you want to protect an object from being modified by external code.

Syntax

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

Reflect.preventExtensions(target)

Parameters

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

  • target − The object on which to prevent extensions.

Return value

This method returns the Boolean value indicating whether the target object was successfully made non-extensible or not.

Examples

Example 1

Let's look at the following example, where the object is initially extensible, later we use the Reflect.preventExtensions() and observe the output.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            a: 11
         };
         document.write(Reflect.isExtensible(x) + " < br > ");
         Reflect.preventExtensions(x); document.write(Reflect.isExtensible(x));
      </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 add a new property after preventing extensions.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            a: 11
         };
         Reflect.preventExtensions(x);
         x.b = 234;
         document.write(JSON.stringify(x));
      </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 modify an existing property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            a: 11
         };
         Reflect.preventExtensions(x);
         x.a = 50;
         document.write(JSON.stringify(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 delete an existing property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            a: 1234
         };
         Reflect.preventExtensions(x);
         delete x.a;
         document.write(JSON.stringify(x));
      </script>
   </body>
</html>

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

Advertisements