Advanced Guide: Reflect
Reflectis a built-in object that provides interceptionJavaScriptHow to operate. These methods are the same as those of proxy handlers.Reflect
Not oneFunction Object, so it is not constructable.
describe
Unlike most global objectsReflect
Not oneConstructor, so it cannot be called through the new operator, orReflect
Objects are called as a function.Reflect
All properties and methods ofMath
Object).
Reflect
The object provides the following static methods, which are related to proxyhandlerThe names of methods are the same.
Some of these methods andObject
The 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 constructornew
Operation is equivalent to executionnew target(...args)
。
(target, propertyKey, attributes)
and()
similar. If the setting is successful, it will returntrue
(target, propertyKey)
As a functiondelete
Operator, 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, andin
The 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
-
const duck = {
-
name: 'Maurice',
-
color: 'white',
-
greeting: function() {
-
(`Quaaaack! My name is ${}`);
-
}
-
}
-
-
(duck, 'color') // true
-
(duck, 'haircut') // false
Returns the object's own properties
-
(duck)
-
// // [ "name", "color", "greeting" ]
Add a new property to this object
(duck, 'eyes', 'black');