ES6 - handler.has()



以下示例定义了一个类 Student,该类有一个构造函数,它将 firstNamelastName 作为参数。此程序创建一个代理,并定义一个处理程序对象。当使用 in 运算符时,就会调用处理程序对象的 has() 方法

<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>

以上代码的输出如下 −

Checking for firstName in the object
true
广告
© . All rights reserved.