當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


ES6 handler.has()用法及代碼示例


下麵的示例定義了一個類 Student,其構造函數采用firstNamelastName作為參數。該程序創建一個代理並定義一個處理程序對象。這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.has()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。