Es6-reflect-get

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

ES6-Reflect.get()

これはプロパティの値を返す関数です。

構文

関数* get()*の構文は次のとおりです。

  • target は、プロパティを取得するターゲットオブジェクトです。
  • propertyKey は取得するプロパティの名前です。
  • Receiver は、ゲッターが検出された場合にターゲットへの呼び出しに提供されるこの値です。 これはオプションの引数です。
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