ES6 - Reflect.get() 函数
这是一个返回属性值的函数。
语法
函数 get() 的语法如下所示,其中,
target 是要获取属性的目标对象。
propertyKey 是要获取的属性的名称。
Receiver 是如果遇到 getter,则为对 target 的调用提供的值。这是一个可选参数。
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

