JavaScript Reflection Learning Tips

JavaScript Reflection Learning Tips

1. Introduction

According to the MDN official website: Reflect is a built-in object that provides methods to intercept JavaScript operations. These methods are the same as for proxy handlers (en-US). Reflect is not a function object, so it is not constructible.

So what is it exactly? According to the above file introduction, it is very similar to Proxy , both of which obtain the information of the execution function itself. The main difference is that all function object properties are too complicated, and adding additional properties may cause unreasonable program behavior, so Reflect function is extended to specifically handle function object calling methods, constructing objects, getting or setting properties and other related operations.

2. Interface

All methods in Reflect are static methods and do not require a constructor or instantiation.

  • Reflect.apply ( target , thisArgument , argumentsList ) calls a function and can pass in an array as a calling parameter. Similar to Function.prototype.apply() .
  • Reflect.construct ( target , argumentsList[, newTarget\] ) performs a new operation on the constructor, which is equivalent to executing new target (...args) .
  • Reflect.defineProperty ( target , propertyKey , attributes ) is similar to Object.defineProperty() . If the setting is successful, it will return true
  • Reflect.deleteProperty(target, propertyKey) as a function's delete operator is equivalent to executing delete target[name] .
  • Reflect.get(target, propertyKey[, receiver\]) gets the value of a property on an object, similar to target[name] .
  • Reflect.getOwnPropertyDescriptor(target, propertyKey) is similar to Object.getOwnPropertyDescriptor() . If the property exists in the object, it returns the corresponding property descriptor, otherwise it returns undefined .
  • Reflect.getPrototypeOf(target) is similar to Object.getPrototypeOf() .
  • Reflect.has(target, propertyKey) determines whether an object has a certain property, which is exactly the same as the in operator.
  • Reflect.isExtensible(target) is similar to Object.isExtensible() .
  • Reflect.ownKeys(target) returns an array containing all own properties (excluding inherited properties). (Similar to Object.keys() , but not affected by enumerable ).
  • Reflect.preventExtensions(target) is similar to Object.preventExtensions() . Returns a Boolean .
  • Reflect.set(target, propertyKey, value[, receiver\]) Function that assigns a value to a property. Returns a Boole that is true if the update was successful.
  • Reflect.setPrototypeOf(target, prototype) sets the prototype function of the object. Returns a Boolean . If the update is successful, it returns true .

3. Simple example

For example, now there is a function:

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName
    this.lastName = lastName
  }
  get getName() {
    return this.firstName + ' ' + this.lastName
  }
}

Normal use only requires instantiation:

const person = new Person('Jaxson', 'Wang')
console.log(person.getName) // Jaxson Wang

You can use the Reflect.construct() method to create an object:

const person = Reflect.construct(Person, ['Jaxson', 'Wang'])
console.log(person) // Jaxson Wang

4. Conclusion

Reflect objects are often used together with Proxy proxies for three reasons:

  • All static methods provided by Reflect are exactly the same as the second handle parameter method Proxy .
  • The return value required by Proxy get/set() method is exactly the return value of Reflect 's get/set method, so they can be used together naturally, which is more convenient and accurate than direct object assignment/requiring values.
  • receiver parameter is irreplaceable.

This is the end of this article about JavaScript reflection learning skills. For more relevant JavaScript reflection content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Java implements reflection json dynamic conversion to entity class--fastjson
  • Java reflection, the use of generics in Json
  • JavaScript reflection and property assignment example analysis
  • How ES6 changes JS's built-in behavior of proxies and reflections
  • Detailed Example of JavaScript Reflection and Dependency Injection
  • JavaScript object reflection usage examples
  • Java reflection to achieve javabean to json example code
  • Introduction to AJAX JavaScript reflection mechanism
  • JS reflection problem

<<:  mysql indexof function usage instructions

>>:  Pay attention to the use of HTML tags in web page creation

Recommend

HTML end tag issue and w3c standard

According to the principles of W3C, each start tag...

Detailed explanation of common Docker commands

1. Help Command 1. View the current Docker versio...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Specific use of the autoindex module in the Nginx Http module series

The main function of the brower module is to dete...

Mysql database scheduled backup script sharing

BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...

Basic usage and pitfalls of JavaScript array sort() method

Preface In daily code development, there are many...

Top 10 Js Image Processing Libraries

Table of contents introduce 1. Pica 2. Lena.js 3....

How to use yum to configure lnmp environment in CentOS7.6 system

1. Installation version details Server: MariaDB S...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...