In the process of learning JavaScript, the cumbersome API is frustrating.

In the case of Object, object.defineProperty (), introduced in ES5, is used to define the Object attribute API, and its usage is clear.

The Proxy proposed in ES6+ plays the same role, or even better. This makes the Cheettos miserable. Again, these questions are often asked in front end interviews.

Hence Reflect. It provides developers with a unified set of apis for manipulating objects. This allows object manipulation to finally have its own set of official standards, and is no longer “tailored to the individual”. At the same time, it also brings some learning costs. So without further ado, let’s brush through some apis.

Reflect static method apply()

var obj = {}
    function fn(q,r){
        console.log(q,r) //"as",23
        console.log("111",this) // {}
        return "hello world"
    }

       let a = Reflect.apply(fn,obj,["as",23])
    console.log(a) // hello world
Copy the code

According to the above code, fn is our target function, obj is the object that replaces this in target, and the elements in [“as”,23] are the parameters in target. The usage is identical to that of function.apply ().

Reflect static method ownKeys()

let obj = {
    name:"xx",
    age:"27",
    sex:1
}
let arr = Reflect.ownKeys(obj)
console.log(arr) // ['name','age','key']
Copy the code

Again, you can see from the result that ownKeys() extracts all the properties in the target object into a single array.

As for the rest of the API, I suggest you go to MDN to brush through.

After brushing through the API, you can see that the usage of objects is very uniform. Reflect is replaced with the reflect.api () structure for reading, deleting, and judging. Such a change, in an instant the previously chaotic usage of the unified management and convention.

This is good news for beginners. Just so you know, EcmaScript also intends to do away with the API, although it’s still on hold, so get familiar with Reflect, otherwise it’s not going to taste good at first 🙂