What is the purpose of the Reflect object in Javascript?

7

Still curious about some news I'm seeing in Javascript, I would now like to know what the purpose of the Reflect object is.

I even found an explanation on MDN , but I did not understand much well the purpose.

Is this Reflect similar to the Reflection of PHP and C #, whose purpose is to do an analysis of the properties or methods of the object? or is it for something else?

    
asked by anonymous 13.07.2018 / 18:22

1 answer

2

Reflect did not bring anything new, just coupled with similar features. It is a union of operators like in and delete , Object functions as defineProperty and getPrototypeOf (with some improvements) among others. The new related methods, which would be added in% with%, will probably only be added in% with%, but those already in Object will not be removed (at least for now) so as not to break codes that use them

Object is an internal object that provides methods for interceptable JavaScript operations. The methods are the same as those of the proxy handlers

  • The Reflect object is not a function object.
  • Does not have an internal method Reflect
  • You can not use the Reflect object as a constructor with the Reflect
  • The Reflect object also does not have an internal method [[Construct]]
  • Can not invoke the Reflect object as a function.
  • All Reflect properties and methods are static (just like Math)

new

In ES5, you typically use [[Call]] to call a function with a given Reflect.apply (target, thisArgument, argumentsList) and arguments as an array (or an array-like object)

With Function.prototype.apply() , this becomes less verbose and easier to understand

this

Prior to the introduction of Reflect, objects could be constructed using an arbitrary combination of constructor and prototype using Reflect.apply and Reflect.construct (target, argumentsList [, newTarget]) . However, while the end result is the same, there is an important difference in the process. When using Object.create() and Function.prototype.apply() , the Object.create() operator will point to Function.prototype.apply() within the function used as constructor, since the new.target keyword is not being used to create the object

On the other hand, invoking undefined , the new operator will point to the Reflect.construct() parameter, if provided, otherwise it will point to new.target

newTarget is NOT the constructor of the Reflect object, it is a static function that is used to create objects as well as the operator target

Reflect.construct()

Similar to new , but returns a Boolean.

The Reflect.defineProperty(target, propertyKey, attributes) , which returns an object if successful, or throws a Object.defineProperty() otherwise you would use a Object.defineProperty block to catch any error that occurred while defining a property. As TypeError returns a Boolean success status, you can simply use a try...catch

Reflect.defineProperty

Allows you to exclude properties, returning a boolean that indicates whether or not the property was deleted successfully. It functions as the if...else operator as a function

Reflect.deleteProperty(target, propertyKey)

Returns an iterator with the enumerable inheritable properties of the target object, but has been removed in ECMAScript 2016 and is deprecated in browsers.

delete

Lets you get a property on an object. It is like the syntax of the property accessor ( Reflect.enumerate(target) or Reflect.get(target, propertyKey [, receptor]) ) as a function

objeto.chave

Similar to objeto['chave'] , however, if the first argument to this method is not an object (a primitive), then it will cause the TypeError. With Reflect.getOwnPropertyDescriptor(target, propertyKey) , a first non-object argument will be coerced to an object first

Object.getOwnPropertyDescriptor

Similar to Object.getOwnPropertyDescriptor , however, if the first argument to this method is not an object (a primitive), then it will cause the TypeError. With Reflect.getPrototypeOf(target) , a first non-object argument will be coerced to an object first

Object.getPrototypeOf

Lets you check if a property is on an object. It functions as the Object.getPrototypeOf operator as a function

Reflect.has(target, propertyKey)

Similar to in , however, if the first argument to this method is not an object (a primitive), then it will cause the TypeError. With Reflect.isExtensible(target) , a first non-object argument will be coerced to an object first

Object.isExtensible

Returns an array with the keys of the properties of an object. It is equivalent to Object.isExtensible ,

Reflect.ownKeys(target)

Similar to Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target)) , however, if the first argument to this method is not an object (a primitive), then it will cause the TypeError. With Reflect.preventExtensions(target) , a first non-object argument will be coerced to an object first

Object.preventExtensions

Lets you set a property on an object. It is like the syntax of the property accessor ( Object.preventExtensions or Reflect.set(target, propertyKey, value[, receiver]) ) as a function

objeto.chave = 'valor'

Similar to objeto['chave'] = 'valor' , allows you to change the prototype (that is, the value of the internal property Reflect.setPrototypeOf(target, prototype) ) of the specified object

    
15.07.2018 / 20:08