Es6-reflect-construct
提供:Dev Guides
ES6-Reflect.construct()
このメソッドはnew演算子として機能し、new target(… args)を呼び出すのと同じです。
構文
以下の構文は、* construct()*関数の構文です。
- target は呼び出すターゲット関数です。
- argumentsList は、ターゲットの呼び出しに使用する引数を指定する配列のようなオブジェクトです。
- newTarget は、そのプロトタイプを使用する必要があるコンストラクターです。 これはオプションのパラメーターです。 このパラメーターに値が渡されない場合、その値は targetparameter です。
Reflect.construct(target, argumentsList[, newTarget])
例
次の例では、fullNameプロパティを持つStudentクラスを作成します。 クラスのコンストラクターは、firstNameおよびlastNameをパラメーターとして受け取ります。 Studentクラスのオブジェクトは、以下に示すようにリフレクションを使用して作成されます。
<script>
class Student{
constructor(firstName,lastName){
this.firstName = firstName
this.lastName = lastName
}
get fullName(){
return `${this.firstName} : ${this.lastName}`
}
}
const args = ['Mohammad','Mohtashim']
const s1 = Reflect.construct(Student,args)
console.log(s1.fullName)
</script>
上記のコードの出力は次のようになります-
Mohammad : Mohtashim