下麵的示例定義了一個類 Student,其構造函數采用firstName和lastName作為參數。該程序創建一個代理並定義一個處理程序對象。這has() method每當使用 in 運算符時都會調用處理程序對象的 。
<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
相關用法
- ES6 handler.set()用法及代碼示例
- ES6 handler.get()用法及代碼示例
- ES6 handler.construct()用法及代碼示例
- ES6 handler.apply()用法及代碼示例
- ES6 RegExp split()用法及代碼示例
- ES6 Array every()用法及代碼示例
- ES6 Array reduceRight()用法及代碼示例
- ES6 Reflect.set()用法及代碼示例
注:本文由純淨天空篩選整理自 ES6 - handler.has()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。