ES6 - Reflect.has()



這是一個作為函式的 in 運算子,它返回一個布林值,指示是否存在自己的屬性或繼承屬性。

語法

以下是函式has()的語法,其中:

  • target 是要查詢屬性的目標物件。

  • propertyKey 是要檢查的屬性名稱。

Reflect.has(target, propertyKey)

示例

以下示例使用反射建立Student類的例項,並使用Reflect.has()方法驗證屬性是否存在。

<script>
   class Student{
      constructor(firstName,lastName){
         this.firstName = firstName
         this.lastName = lastName
      }
      get fullName(){
         return `${this.firstName} : ${this.lastName}`
      }
   }

   const args = ['Tutorials','Point']
   const s1 = Reflect.construct(Student,args)
   console.log(Reflect.has(s1,'fullName'))
   console.log(Reflect.has(s1,'firstName'))
   console.log(Reflect.has(s1,'lastname'))
</script>

以上程式碼的輸出如下:

true
true
false
廣告