Reflect
Reflect is a built-in object that provides methods to intercept JavaScript operations. These methods are the same as those of proxy handlers .
Reflect
is not a function object, so it is not constructible.
Unlike most global objects Reflect
, it is not a constructor, so it cannot be called via the new operator , or the Reflect
object as a function. Reflect
All properties and methods of are static (like Math
objects).
Reflect
The object provides the following static methods, named the same as the proxy handler methods (en-US) .
Some of these methods are the Object
same as , although there are some subtle differences between the two
static method
-
Reflect.apply(target, thisArgument, argumentsList)
To call a function, you can pass an array as a call parameter.
Function.prototype.apply()
Similar to function . -
Reflect.construct(target, argumentsList[, newTarget\])
new
Operating on the constructor is equivalent to executingnew target(...args)
. -
Reflect.defineProperty(target, propertyKey, attributes)
and
Object.defineProperty()
similar . If the setting is successful, it will returntrue
-
Reflect.deleteProperty(target, propertyKey)
As a function
delete
operator, it is equivalent to executedelete target[name]
. -
Reflect.get(target, propertyKey[, receiver\])
Get the value of a property on an object, similar to
target[name]。
-
Reflect.getOwnPropertyDescriptor(target, propertyKey)
Similar to
Object.getOwnPropertyDescriptor()
. If the property exists in the object, return the corresponding property descriptor, otherwise return itundefined
. -
Reflect.getPrototypeOf(target)
Similar to
Object.getPrototypeOf()
. -
Reflect.has(target, propertyKey)
Determining whether an object has an
in
attribute has exactly the same function as the operator. -
Similar to
Object.isExtensible()
. -
Returns an array containing all own properties (excluding inherited properties). (similar to
Object.keys()
, but not affected byenumerable影响
). -
Reflect.preventExtensions(target)
Similar to
Object.preventExtensions()
. return oneBoolean
. -
Reflect.set(target, propertyKey, value[ receiver])
A function to assign a value to a property. Returns one
Boolean
, or if the update was successfultrue
. -
Reflect.setPrototypeOf(target, prototype)
A function to set the prototype of the object. Returns one
Boolean
, or if the update was successfultrue。
example
Check if an object has a specific property
const apiJson = {
name: 'static data',
type: 'static',
getData: function() {
console.log(`获取的数据类型为:${
this.name}`);
}
}
Reflect.has(apiJson, 'type'); // true
Reflect.has(apiJson, 'age'); // false
Add a new property to this object:
Reflect.set(apiJson, 'data', '静态数据1');
Return the properties of this object itself:
Reflect.ownKeys(apiJson)
// 检测一个对象是否存在特定属性
const apiJson = {
name: 'static data',
type: 'static',
getData: function() {
console.log(`获取的数据类型为:${
this.name}`);
}
}
console.log(Reflect.has(apiJson, 'type')); // true
console.log(Reflect.has(apiJson, 'age')); // false
// Reflect.set(apiJson, 'data', '静态数据1');
console.log(Reflect.set(apiJson, 'data', '静态数据1')); // true
// 返回这个对象自身的属性
console.log(Reflect.ownKeys(apiJson));