Es6-handler-has

提供:Dev Guides
移動先:案内検索

ES6-handler.has()

次の例では、 firstName および lastName をパラメーターとして取るコンストラクターを使用してStudentクラスを定義しています。 プログラムはプロキシを作成し、ハンドラオブジェクトを定義します。 ハンドラーオブジェクトの* has()メソッド*は、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