Reflect ion object of ES new feature

Posted by gojiita on Wed, 15 Dec 2021 13:40:56 +0100

Hello, I'm A bowl week , a front end that doesn't want to be drunk. If you are lucky enough to get your favor, I am very lucky~

summary

Reflect is an object provided by ECMAScript2015. It provides some static methods to intercept JavaScript operations. These methods are similar to Proxy The methods in handlers in are consistent.

Reflect is not a constructor, that is, it cannot be instantiated.

For each interception operation (e.g. get, delete, etc.) in the Proxy object, the reflection method is called internally. The static method provided is the same as Proxy The method names in the handlers in are consistent, as follows:

Default tonefunction
Reflect.get()Gets the value of a property on the object
Reflect.set()Set properties on object
Reflect.has()Determine whether an object has a property
Reflect.deleteProperty()Delete attribute on object
Reflect.getPrototypeOf()Gets the function of the specified object prototype
Reflect.setPrototypeOf()Functions that set or change object prototypes
Reflect.isExtensible()Judge whether an object is extensible (that is, whether new attributes can be added)
Reflect.preventExtensions()Prevent new properties from being added to the object
Reflect.getOwnPropertyDescriptor()Gets the property descriptor for the given property
Reflect.defineProperty()Define or modify the properties of an object
Reflect.ownKeys()Returns an array of property keys of the target object itself
Reflect.apply()When calling a function, you can pass in an array as the call parameter
Reflect.construct()Carry out new operation on the constructor to create an instance of the class

Static method

Now let's use these static methods in this way

get

Reflect. The syntax structure of the get () method is as follows:

Reflect.get(target, key)

This method is used to obtain the value with the attribute key from the target object. The example code is as follows:

const person = {
    name: 'A bowl week'
}

console.log(Reflect.get(person, 'name')) // A bowl week

It is worth noting that if the target is not an object, an exception will be thrown

set

Reflect. The syntax of the set () method is as follows:

Reflect.set(target, key, value)

This method is used to set the key value for the key attribute in the target object and return a Boolean value indicating whether the setting is successful.

The example code is as follows:

const person = {}

console.log(Reflect.set(person, 'name', 'Prosperity on the other side')) // true

It is worth noting that if the target is not an object, an exception will be thrown

has

Reflext. The syntax structure of the has () method is as follows:

Reflect.has(target, key)

This method is used to detect whether the attribute key belongs to the attribute in the target object, and returns a Boolean value indicating whether it contains.

The example code is as follows:

const person = {
    name: 'A bowl week'
}


console.log(Reflect.has(person, 'name')) // true
console.log(Reflect.has(person, 'hobby')) // false

deleteProperty

Reflect. The syntax structure of the deleteproperty () method is as follows:

Reflect.deleteProperty(target, key)

This method is used to delete the key attribute of the target object and return a Boolean value indicating whether the deletion was successful. The example code is as follows:

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}


console.log(Reflect.deleteProperty(person, 'hobby')) // true
console.log(Reflect.has(person, 'hobby')) // false
console.log(person) // {name: 'one bowl week'}

getPrototypeOf

Reflect. The getprototypeof (target) method is used to obtain the prototype of the target object. The example code is as follows:

const person = {
    name: A bowl week',
    hobby: 'coding'
}

// Get the prototype of person
const proto = Reflect.getPrototypeOf(person)
console.log(proto) // [Object: null prototype] {}

setPrototypeOf

Reflect. The syntax structure of setprototypeof() method is as follows:

Reflect.setPrototypeOf(target, prototype)

The function of this method is to modify the prototype of the target object to prototype and return a Boolean value indicating whether the modification was successful. The example code is as follows:

class sayMe {
}

// Set up a method on the prototype
sayMe.prototype.print = function () {
    console.log('A bowl week');
}

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}


const proto = Reflect.setPrototypeOf(person, sayMe)
console.log(proto) // true

// You can call the method above the prototype
person.prototype.print() // Prosperity on the other side

preventExtensions and isExtensible

Reflect. The preventextensions () method is used to change the target object into a non extensible object and return a Boolean value indicating whether the setting is successful.

Reflect.isExtensible(target) method is used to judge whether the target object can be extended, that is, whether new attributes can be added. The return value of this method is a Boolean value, indicating whether the object can be extended.

The example code is as follows:

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}
console.log(Reflect.isExtensible(person)) // true

// Through object Preventextensions() makes the object non extensible
Object.preventExtensions(person)


console.log(Reflect.isExtensible(person)) // false

getOwnPropertyDescriptor

Reflect. The syntax structure of getownpropertydescriptor () method is as follows:

Reflect.getOwnPropertyDescriptor(target, key)

This method will return the attribute descriptor corresponding to a self owned attribute key on the target object (the so-called self owned attribute refers to the attribute directly assigned to the object, which does not need to be searched from the prototype chain).

The example code is as follows:

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}
console.log(Reflect.getOwnPropertyDescriptor(person, 'name')) // {value: 'a bowl week', writable: true, enumerable: true, configurable: true}

defineProperty

Reflect. The syntax structure of the defineproperty () method is as follows:

Reflect.defineProperty(target, key, attributes)

This method is used to modify or define the attribute descriptor of the key attribute in the target object. attributes represents an attribute descriptor object.

There are four values in the attribute descriptor:
Value: the value corresponding to this attribute
Writable: writable
configurable: whether it can be changed
Enumerable: enumerable

The example code is as follows:

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}
Reflect.defineProperty(person, 'name', {
    writable: false
})
person.name = 'A cup of porridge'

// name modification failed
console.log(person);

ownKeys

Reflect. The ownkeys (target) method returns an array of self owned attribute keys in the target object. The example code is as follows:

const person = {
    name: 'A bowl week',
    hobby: 'coding'
}
console.log(Reflect.ownKeys(person)) // [ 'name', 'hobby' ]

apply

Reflect. The syntax structure of apply() is as follows:

Reflect.apply(func, thisArg, args)

The function is to call func function and pass an args argument list, which is an array like object; thisArg represents the this object when called.

This method is equivalent to function prototype. apply. call(func, thisArg, args)

The example code is as follows:

const person = {
    name: 'A bowl week',
    sayMe: function () {
        console.log(this.name);
    }
}
// Note: even if there are no parameters, the third parameter must be passed
Reflect.apply(person.sayMe, person, [])

It is worth noting that even if there is no third parameter, an empty array needs to be passed in

construct

Reflect. The function of the construct () method is actually to create an instance object. The syntax structure of this method is as follows:

Reflect.construct(target, argumentsList[, newTarget])

Parameter Description:

  • target: represents the constructor that can be run

  • argumentsList: represents the parameters when called by the constructor. This is a class array object

  • newTarget: This is optional and represents the constructor property of the newly created prototype object. The default value is target

The example code is as follows:

function Person (name, hobby) {
    this.name = name
    this.hobby = hobby
    this.sayMe = function () {
        console.log(`${this.name}like ${this.hobby}`);
    }
}

// Through reflect Construct () instantiates an object
const person = Reflect.construct(Person, ['A bowl week', 'coding'])

person.sayMe() // A bowl of Zhou likes coding

summary

The static methods provided by the Reflect object can be completed in another way, so why provide a Reflect object?

This is because the previous 13 operations may come from methods in different objects, including global, Object and Function. However, after providing the Reflect Object, all the previous operations are unified under one Object and the operation mode is unified.

Previous recommendation

Topics: Big Data ElasticSearch