Purpose of Reflect: Exploring Properties
Learn how the Reflect class helps access and explore properties in JavaScript.
Getting and setting properties
In Dynamic Access, we explored ways to dynamically access properties. Reflect
provides alternatives to both get
and set
properties.
Example
Let’s take a look at the Person
class:
Press + to interact
'use strict';//START:PROPERTIESclass Person {constructor(age) {this.age = age;}play() { console.log(`The ${this.age} year old is playing`); }get years() { return this.age; }}//END:PROPERTIES
For example, to access the age
property on an instance of sam
of Person
, we can perform sam.age
.
Reflect
's get
and set
If we don’t know the name of the property at code writing time, we can pass the property name as a string to Reflect
’s ...
Access this course and 1400+ top-rated courses and projects.