此方法充當 new 運算符,等效於調用 new target(...args)。
用法
下麵給出的語法用於函數construct(),其中,
target是要調用的目標函數。
argumentsList是一個 array-like 對象,指定應使用哪個目標調用的參數。
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
相關用法
- ES6 Reflect.set()用法及代碼示例
- ES6 Reflect.apply()用法及代碼示例
- ES6 Reflect.has()用法及代碼示例
- ES6 Reflect.get()用法及代碼示例
- ES6 RegExp split()用法及代碼示例
- ES6 RegExp replace()用法及代碼示例
- ES6 RegExp exec()用法及代碼示例
- ES6 RegExp test()用法及代碼示例
- ES6 RegExp toString()用法及代碼示例
- ES6 RegExp search()用法及代碼示例
- ES6 Array every()用法及代碼示例
- ES6 Array reduceRight()用法及代碼示例
- ES6 handler.set()用法及代碼示例
注:本文由純淨天空篩選整理自 ES6 - Reflect.construct()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。