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

Vue+flask realizes video synthesis function (drag and drop upload)

Table of contents We have written about drag and ...

SQL implementation of LeetCode (197. Rising temperature)

[LeetCode] 197.Rising Temperature Given a Weather...

JavaScript dynamically generates a table with row deletion function

This article example shares the specific code of ...

Detailed tutorial on installing Docker and nvidia-docker on Ubuntu 16.04

Table of contents Docker Installation Nvidia-dock...

How to create a trigger in MySQL

This article example shares the specific code for...

Parsing Linux source code epoll

Table of contents 1. Introduction 2. Simple epoll...

How to use CocosCreator for sound processing in game development

Table of contents 1. Basics of audio playback in ...

What is BFC? How to clear floats using CSS pseudo elements

BFC Concept: The block formatting context is an i...

Docker-compose image release process analysis of springboot project

Introduction The Docker-Compose project is an off...

MySQL dual-machine hot standby implementation solution [testable]

Table of contents 1. Concept 2. Environmental Des...

VMware Workstation 14 Pro installs CentOS 7.0

The specific method of installing CentOS 7.0 on V...

How to View All Running Processes in Linux

You can use the ps command. It can display releva...