What is the purpose of the Reflect object in Javascript?

Asked

Viewed 307 times

7

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

I even found an explanation in MDN, but I didn’t quite understand the purpose.

Would that be Reflect similar to the Reflection PHP and C#, which aims to perform an analysis of the properties or methods of the object? or is for something else?

  • 1

    From what I understand, it’s a union of operators like in and delete, functions of Object as defineProperty and getPrototypeOf with some improvements and some new features. The new related methods, which, before, would be added in Object, will probably be added only in Reflect fountain

  • 1

    Very interesting question!!! Briefly: it serves to intercept operations in Javascript, but I’ll wait for the answers Ctrl+C to appear to try something, maybe in the meantime and in the middle of the weak answers a great :) - ps: Reflect is not a constructor, its methods are static.

  • 1

    @Guilhermenascimento can now make his answer?

1 answer

2

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

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

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

Reflect.apply (target, thisArgument, argumentsList)

At ES5, you usually use the Function.prototype.apply() to call a function with a given this and arguments as a matrix (or an object similar to a matrix)

With Reflect.apply, this becomes less detailed and easier to understand

Reflect.construct (target, argumentsList [, newTarget])

Before the introduction of Reflect, objects could be constructed using an arbitrary combination of constructor and prototype using Object.create() and Function.prototype.apply(). 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 operator new.target will point to undefined within the function used as constructor, once the keyword new is not being used to create the object

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

Reflect.construct() NOT the constructor of the Reflect object, it is a static function that serves to create objects as well as the operator new

Reflect.defineProperty(target, propertyKey, attributes)

Similar to Object.defineProperty(), but returns a Boolean.

The Object.defineProperty, which returns an object if successful, or launches a TypeError otherwise you would use a block try...catch to capture any error that occurred during setting a property. How Reflect.defineProperty returns a boolean success status, you can simply use a if...else

Reflect.deleteProperty(target, propertyKey)

Lets you delete properties by returning a Boolean that indicates whether or not the property has been deleted successfully. Works as the operator delete as a function

Reflect.enumerate(target)

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

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

Allows you to get a property on an object. It’s like the properties access syntax (objeto.chave or objeto['chave']) as a function

Reflect.getOwnPropertyDescriptor(target, propertyKey)

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

Reflect.getPrototypeOf(target)

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

Reflect.has(target, propertyKey)

Allows checking if a property is in an object. Acts as the operator in as a function

Reflect.isExtensible(target)

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

Reflect.ownKeys(target)

Returns an array with the keys of an object’s properties. It is equivalent to Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target)),

Reflect.preventExtensions(target)

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

Reflect.set(target, propertyKey, value[, receiver])

Allows you to define a property on an object. It’s like the properties access syntax (objeto.chave = 'valor' or objeto['chave'] = 'valor') as a function

Reflect.setPrototypeOf(target, prototype)

Similar to Object.setPrototypeOf, allows you to change the prototype (i.e., the value of the internal property [[Prototype]]) of the specified object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.