ES6 - Reflect.get()



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

語法

下面給出該函式get()的語法,其中:

  • target 是要獲取屬性的目標物件。

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

  • Receiver 是如果遇到 getter,則為對 target 的呼叫提供的 this 值。這是一個可選引數。

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

示例

下面的示例使用反射建立 Student 類的例項,並使用Reflect.get() 方法獲取例項的屬性。

<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
廣告