Home > Web Front-end > JS Tutorial > body text

Detailed introduction to Reflect in JavaScript (with examples)

不言
Release: 2018-12-10 17:58:44
forward
4487 people have browsed it

This article brings you a detailed introduction to Reflect in JavaScript (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Reflect is a built-in object that provides methods to intercept JavaScript operations. The method is the same as that of the proxy handler. Reflect is not a function object, so it is not constructible.

    new Reflect() //错误的写法
Copy after login

Reflect uses
Reflect provides some static methods. Static methods refer to methods that can only be accessed through the object itself.
1, Reflect.apply()
2, Reflect.construct ()
3、Reflect.defineProperty()
4、Reflect.deleteProperty()
5、Reflect.enumerate()
6、Reflect.get()
7、Reflect.getOwnPropertyDescriptor ()
8、Reflect.getPrototypeOf()
9、Reflect.has()
10、Reflect.isExtensible()
11、Reflect.ownKeys()
12、Reflect.preventExtensions ()
13, Reflect.set()
14, Reflect.setPrototypeOf()

Use of static methods:
demo1: Use Reflect.get() to obtain the specified key of the target object value.

let obj = {
    a: 1
};

let s1 = Reflect.get(obj, "a")
console.log(s1) // 1
Copy after login

The get() method of demo1 intercepts the obj object and then reads the value with key a. Of course, the value of a can also be read without Reflect.

demo2: Use Reflect.apply to pass in the specified parameters to the target function floor.

const s2 = Reflect.apply(Math.floor, undefined, [1.75]); 
console.log(s2) // 1
/ /Reflect.apply()提供了3个参数,
// 第一个参数是反射的函数,后面2个参数才是和数组的apply一致。
Copy after login

The example of demo2 can be understood as intercepting the Math.floor method, then passing in the parameters and assigning the return value to s2, so that we can call s2 when we need to read the return value.

demo3: Use Reflect.ownKeys to get the keys of the object

    console.log(Reflect.ownKeys({"a":0,"b":1,"c":2,"d":3})); //输出 :["a", "b", "c", "d"]
    console.log(Reflect.ownKeys([])); // ["length"]      
    var sym = Symbol.for("comet");      
    var sym2 = Symbol.for("meteor");      
    var obj = {
        [sym]: 0, "str": 0, "773": 0, "0": 0,      
        [sym2]: 0, "-1": 0, "8": 0, "second str": 0
    };      
    Reflect.ownKeys(obj);
     //输出:/ [ "0", "8", "773", "str", "-1", "second str", Symbol(comet), Symbol(meteor) ]
Copy after login

The sorting of Reflect.ownKeys is based on: display the numbers first, sort the numbers according to size, and then sort the strings according to the order of insertion

The above is the detailed content of Detailed introduction to Reflect in JavaScript (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template