The Reflect API is a global object, but it is slightly different than other global objects. Reflect is not a function object, so you are unable to construct it. This means you cannot use the new operator or call Reflect as a function. This is similar to the Math and JSON objects. Methods on the Reflect object are static. The Reflect object contains the same methods as the prototypical methods on Object, but there are slight differences. Let’s explore some reflect methods.
Reflect.apply(target, thisArgument, argumentsList) – used to call a target function with arguments specified by the argumentsList parameter. It is similar to Function.prototype.apply().
Reflect.construct(target, argumentsList[, newTarget] – acts like the new operator, but as a function. It is the same as calling new target(…arguments). You can also specify a different prototype if needed.
Reflect.defineProperty(target, propertyKey, attributes) – returns a Boolean that is true if the property was defined successfully. It is similar to Object.defineProperty().
Reflect.deleteProperty(target, propertyKey) – this is the delete operator as a function. It is the same as calling delete target[propertyKey].
Reflect.get)target, propertyKey[, receiver]) – returns the value of the property. It is the same as getting a property from an object target[propertyKey] as a function.
Reflect.getOwnPropertyDescriptor(target, propertyKey) – returns a property descriptor of the given property if it exists on the object and returns undefined otherwise. It is similar to Object.getOwnPropertyDescriptor().
Reflect.getPropertyOf(target) – same as Object.getPrototypeOf().
Reflect.has(target, propertyKey) – returns a Boolean depending on whether the target has the property or not. The target can have the property as its own or inherited. This method works like the ‘in operator’ as a function.
Reflect.isExtensible(target) – returns a Boolean that is true if the target is extensible. This works the same as Object.isExtensible().
Reflect.ownKeys(target) – returns an array of the target object’s own property keys. The property keys cannot be inherited.
Reflect.preventExtensions(target) – returns a Boolean that is true if the update was successful.
Reflect.set(target, propertyKey, value[, receiver] – a function that assigns values to properties and returns a Boolean that is true if the update was successful.
Reflect.setPrototypeOf(target, prototype) – a function that sets the prototype of an object and returns a Boolean that is true if the update was successful.
(MDN Reflect, 2020)
Let’s look at some examples to show all of the many features of the Reflect API. Below, we have a dog object. Lets check to see if we have a specific key.
Using the same object, we will also return the object's own keys and add a new property.
Lastly, we will construct an object using Reflect.