ES6 - Reflect.get()



This is a function that returns the value of properties.

Syntax

The syntax for the function get() is given below, where,

  • target is the target object on which to get the property.

  • propertyKey is the name of the property to get.

  • Receiver is the value of this provided for the call to target if a getter is encountered. This is an optional argument.

Reflect.get(target, propertyKey[, receiver])

Example

The following example creates an instance of the class Student using reflection and fetches the properties of the instance using the 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>

The output of the above code will be as shown below −

fullname is Tutorials : Point
firstName is Tutorials
Advertisements