web123456

Vue 3 Advanced Guide Reflect

Advanced Guide: Reflect

Reflectis a built-in object that provides interceptionJavaScriptHow to operate. These methods are the same as those of proxy handlers.ReflectNot oneFunction Object, so it is not constructable.

describe

Unlike most global objectsReflectNot oneConstructor, so it cannot be called through the new operator, orReflectObjects are called as a function.ReflectAll properties and methods ofMathObject).

ReflectThe object provides the following static methods, which are related to proxyhandlerThe names of methods are the same.

Some of these methods andObjectThe same, although there are some subtle differences between the two.

Static method

(target, thisArgument, argumentsList)

Call a function, and you can pass in oneArrayAs a call parameter. and()Similar functions.

(target, argumentsList[, newTarget\])

Perform the constructornewOperation is equivalent to executionnew target(...args)

(target, propertyKey, attributes)

and()similar. If the setting is successful, it will returntrue

(target, propertyKey)

As a functiondeleteOperator, equivalent to executiondelete target[name]

(target, propertyKey[, receiver\])

Get the value of a property on the object, similar totarget[name]。

(target, propertyKey)

Similar to(). If this property exists in the object, the corresponding property descriptor will be returned, otherwiseundefined.

(target)

Similar to()

(target, propertyKey)

Determine whether an object has a certain property, andinThe functions of the operator are exactly the same.

(target)

Similar to().

(target)

Returns an array containing all its own attributes (not including inherited attributes). (similar to(), but won't accept itenumerable impact).

(target)

Similar to(). Return oneBoolean

(target, propertyKey, value[, receiver\])

Function that assigns values ​​to attributes. Return oneBoolean, if the update is successful, returntrue

(target, prototype)

Function that sets the object prototype. Returns aBoolean, if the update is successful, returntrue。

Practical examples

Detect whether an object has a specific attribute

  1. const duck = {
  2.   name: 'Maurice',
  3.   color: 'white',
  4.   greeting: function() {
  5.     (`Quaaaack! My name is ${}`);
  6.   }
  7. }
  8. (duck, 'color'// true
  9. (duck, 'haircut'// false

Returns the object's own properties

  1. (duck)
  2. // // [ "name", "color", "greeting" ]

Add a new property to this object

(duck, 'eyes''black');