ES6 - handler.has()
以下示例定义了一个 Student 类,该类的构造函数以 firstName 和 lastName 为参数。该程序创建了一个代理并定义了一个处理程序对象。每当使用 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

