ES6 - handler.has()



The following example defines a class Student with a constructor that takes firstName and lastName as parameters. The program creates a proxy and defines a handler object. The has() method of the handler object is called whenever the in operator is used.

<script>
   class Student{
      constructor(firstName,lastName){
         this.firstName = firstName
         this.lastName = lastName
      }
   }
   const handler = {
      has: function(target,property){
         console.log('Checking for '+property+' in the object')
         return Reflect.has(target,property)
      }
   }

   const s1 = new Student("Tutorials","Point")
   const proxy = new Proxy(s1,handler)
   console.log('firstName' in proxy)
</script>

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

Checking for firstName in the object
true
Advertisements