Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Reflexive Learning method of JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the reflective learning method of JavaScript". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the reflective learning method of JavaScript".

1. Preface

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

So what is it? According to the above document introduction, you will find that it is very similar to Proxy, which is to get the information of the execution function itself. The main difference is that all the function object properties are too complex, and the extra addition may lead to unreasonable program behavior, so extend the Reflect function to deal with calling methods, constructing objects, getting or setting properties and other related operations.

2. Interface

All methods in Reflect are static, and there is no need to construct a function or instantiate it.

Reflect.apply (target, thisArgument, argumentsList), call a function, and pass in an array as the call parameter. The function is similar to Function.prototype.apply ().

Reflect.construct (target, argumentsList [, newTarget\]) new the constructor, which is equivalent to performing new target (... args).

Reflect.defineProperty (target, propertyKey, attributes) is similar to Object.defineProperty (). True will be returned if the setting is successful.

Reflect.deleteProperty (target, propertyKey), as the delete operator of a function, is equivalent to executing delete target [name].

Reflect.get (target, propertyKey [, receiver\]) gets the value of a property on the object, similar to target [name].

Reflect.getOwnPropertyDescriptor (target, propertyKey) is similar to Object.getOwnPropertyDescriptor (). If the property exists in the object, the corresponding property descriptor is returned, otherwise undefined.

Reflect.getPrototypeOf (target) is similar to Object.getPrototypeOf ().

Reflect.has (target, propertyKey) determines whether an object has an attribute, which is exactly the same as the function of the in operator.

Reflect.isExtensible (target) is similar to Object.isExtensible ().

Reflect.ownKeys (target) returns an array of all its 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\]) assigns the value to the function of the attribute. Returns a Boolean or true if the update is successful.

Reflect.setPrototypeOf (target, prototype) sets the function of the object prototype. Returns a Boolean or true if the update is successful.

3. Simple examples

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}}

For normal use, you only need to instantiate:

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 Wang4, conclusion

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

All the static methods provided by Reflect are identical to the second handle parameter method of Proxy.

The return value required by the Proxy get/set () method is the return value of Reflect's get/set method, which can be used naturally and is more convenient and accurate than directly assigning / fetching values from objects.

The receiver parameter is irreplaceable.

Thank you for your reading, the above is the content of "the reflective learning method of JavaScript". After the study of this article, I believe you have a deeper understanding of the reflective learning method of JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report