JavaScript Reflection Learning Skills
- 2021-11-29 05:58:52
- OfStack
1. Preface
According to MDN official website explanation:
Reflect
Is a built-in object that provides interception
JavaScript
The method of operation. These methods are the same as those of proxy handlers (en-US).
Reflect
Is not a function object, so it is not constructible.
So what exactly is it? According to the above file, you will find that it and
Proxy
Extremely similar, they all get the information of the execution function itself. The main difference is that all the function object attributes are too complex, and the extra addition may lead to unreasonable program behavior, so the extension
Reflect
Function to deal specifically with function object processing, such as calling methods, constructing objects, obtaining or setting properties, and other related operations.
2. Interface
Reflect
All the methods in it are static methods, and there is no need to construct or instantiate it.
Reflect.apply
(
target
,
thisArgument
,
argumentsList
), call a function and pass in an array as the call parameter. And
JavaScript
0
The function is similar.
JavaScript
1
(
target
,
JavaScript
3
) Performs an new operation on the constructor, which is equivalent to executing the
JavaScript
4
(... args).
JavaScript
5
(
target
,
JavaScript
7
,
JavaScript
8
) and
JavaScript
9
Similar. If the setting is successful, it will return
Reflect
0
Reflect
1
As a function
Reflect
2
Operator, which is equivalent to executing the
Reflect
3
.
Reflect
4
Gets the value of a property on an object, similar to
Reflect
5
.
Reflect
6
Similar to
Reflect
7
. Returns the corresponding property descriptor if the property exists in the object, otherwise it returns
Reflect
8
.
Reflect
9
Similar to
Proxy
0
.
Proxy
1
Determines whether an attribute exists in 1 object, and
Proxy
2
Operator functions exactly the same.
Reflect.isExtensible(target)
Similar to
Object.isExtensible() .
Reflect.ownKeys(target)
Returns an array containing all its own attributes (excluding inherited attributes). (Similar to
Object.keys()
, but will not be affected by
enumerable
Influence).
Reflect.preventExtensions(target)
Similar to
Object.preventExtensions()
. Returns 1
Boolean
.
Reflect.set(target, propertyKey, value[, receiver\])
A function that assigns a value to a property. Returns 1
Boole
an, if the update is successful, returns
true
.
Reflect.setPrototypeOf(target, prototype)
The function that sets the prototype of the object. Returns 1
Boolean
If the update is successful, return the
Reflect
0
.
3. Simple examples
For example, there is a function now:
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
get getName() {
return this.firstName + ' ' + this.lastName
}
}
Normal use only needs to be instantiated:
const person = new Person('Jaxson', 'Wang')
console.log(person.getName) // Jaxson Wang
You can use the Reflect. construct () method to create objects:
const person = Reflect.construct(Person, ['Jaxson', 'Wang'])
console.log(person) // Jaxson Wang
4. Conclusion
Reflect objects are often used with Proxy proxies for three reasons:
Reflect
All static methods provided by the
Proxy
The second
handle
The parametric method is one model and one sample.
Reflect
0
The return value required by the method is exactly the
Reflect
1
Adj.
Reflect
2
Method, it can be used naturally, and it is more convenient and accurate than direct object assignment/retrieval.
Reflect
3
Parameters are irreplaceable.