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


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

相關用法


注:本文由純淨天空篩選整理自 ES6 - Reflect.has()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。