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


ES6 Reflect.get()用法及代碼示例


這是一個返回屬性值的函數。

用法

函數的語法get()下麵給出,其中,

  • target是在其上獲取屬性的目標對象。

  • propertyKey是要獲取的屬性的名稱。

  • Receiver如果遇到 getter,則是為調用 target 提供的 this 值。這是一個可選參數。

Reflect.get(target, propertyKey[, receiver])

示例

下麵的示例使用反射創建類 Student 的實例,並使用Reflect.get() method

<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('fullname is ',Reflect.get(s1,'fullName'))

   console.log('firstName is ',Reflect.get(s1,'firstName'))
</script>

上麵代碼的輸出將如下所示——

fullname is Tutorials:Point
firstName is Tutorials

相關用法


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