這是一個返回屬性值的函數。
用法
函數的語法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.set()用法及代碼示例
- ES6 Reflect.construct()用法及代碼示例
- ES6 Reflect.apply()用法及代碼示例
- ES6 Reflect.has()用法及代碼示例
- 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.get()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。