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

In-depth explanation of the global status of WeChat applet

Preface In WeChat applet, you can use globalData ...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

FlashFXP ftp client software registration cracking method

The download address of FlashFXP is: https://www....

A brief understanding of the three uses of standard SQL update statements

1. Environment: MySQL-5.0.41-win32 Windows XP Pro...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...

Analyze the working principle of Tomcat

SpringBoot is like a giant python, slowly winding...

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

A brief introduction to MySQL InnoDB ReplicaSet

Table of contents 01 Introduction to InnoDB Repli...

Detailed tutorial on docker-compose deployment and configuration of Jenkins

Docker-compose deployment configuration jenkins 1...

Docker installation of Nginx problems and error analysis

question: The following error occurred when insta...

A brief explanation of the reasonable application of table and div in page design

At the beginning of this article, I would like to ...

CSS text alignment implementation code

When making forms, we often encounter the situati...

MySQL whole table encryption solution keyring_file detailed explanation

illustrate MySql Community Edition supports table...

Analysis of the solution to Nginx Session sharing problem

This article mainly introduces the solution to th...