Getting to know Reflect

Reflect

Reflect is a built-in object that provides methods to intercept JavaScript operations. These methods are the same as those of proxy handlers . Reflectis 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 Reflectobject as a function. ReflectAll properties and methods of are static (like Mathobjects).

ReflectThe object provides the following static methods, named the same as the proxy handler methods (en-US) .

Some of these methods are the Objectsame as , although there are some subtle differences between the two

static method

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

insert image description here

Add a new property to this object:

Reflect.set(apiJson, 'data', '静态数据1');

insert image description here

Return the properties of this object itself:

Reflect.ownKeys(apiJson)

insert image description here

// 检测一个对象是否存在特定属性
  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));

For detailed documentation, please check MDN: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Reflect

Guess you like

Origin blog.csdn.net/weixin_43853746/article/details/122659488