ES6 - Reflect.has()



This is the in operator as a function which returns a boolean indicating whether an own or inherited property exists.

Syntax

Given below is the syntax for the function has(), where,

  • target is the target object in which to look for the property.

  • propertyKey is the name of the property to check.

Reflect.has(target, propertyKey)

Example

The following example creates an instance of the class Student using reflection and verifies if the properties exist using the Reflect.has() method.

<script>
   class Student{
      constructor(firstName,lastName){
         this.firstName = firstName
         this.lastName = lastName
      }
      get fullName(){
         return `${this.firstName} : ${this.lastName}`
      }
   }

   const args = ['Tutorials','Point']
   const s1 = Reflect.construct(Student,args)
   console.log(Reflect.has(s1,'fullName'))
   console.log(Reflect.has(s1,'firstName'))
   console.log(Reflect.has(s1,'lastname'))
</script>

The output of the above code will be as mentioned below −

true
true
false
Advertisements