• JavaScript Video Tutorials

JavaScript - Reflect.has() Method



The Reflect.has() method is used to find out if a target object has a particular property or not. It is similar to the Object.prototype.hasOwnProperty() method or the in operator, but it offers a more flexible and consistent means of performing property checks. The Reflect.has() method is not a function but a static method, so you should not use it as a function.

In contrast to Object.prototype.hasOwnProperty(), Reflect.has() does not raise an error if the target object is null or undefined. In certain situations, it returns false instead.

Syntax

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

Reflect.has(target, propertyKey)

Parameters

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

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

  • propertyKey − Name of the property to check.

Return value

This method returns the Boolean value indicating if the target has property or not.

Examples

Example 1

Let's look at the following example, where we are going to use the Reflect.has() and check whether the object has properties or not.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            car: 'BMW',
            Model: 2018
         };
         document.write(Reflect.has(x, 'Model') + " < br > ");
         document.write(Reflect.has(x, 'spare'));
      </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 set configurable to false and try to delete the property and retrieving the output using Reflect.has().

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {};
         Object.defineProperty(x, 'Bike', {
            value: 'Rx100',
            configurable: false
         });
         delete x.Bike;
         document.write(Reflect.has(x, 'Bike'));
      </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 use the Reflect.has() to check for the existence of a symbol property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = Symbol('Tp');
         const y = {
            [x]: 'Welcome'
         };
         document.write(Reflect.has(y, 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 use the Reflect.has() for a Inherited Property.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         class x {
            constructor() {
               this.a = 11;
            }
         }
         class y extends x {
            constructor() {
               super();
               this.b = 22;
            }
         }
         const z = new y();
         document.write(Reflect.has(z, 'a'));
      </script>
   </body>
</html>

On running the above code, the output window will pop up, displaying text on the webpage.

Advertisements