This is the 24th day of my participation in Gwen Challenge

preface

Figure from gold nuggets information section.

I arrived at the office at 11 o ‘clock and just sat down and opened my browser and started fishing. When I opened the nuggets information section, I saw the news above.

How could I not learn some new features?

ES12 cutting-edge new features

String.prototype.replaceAll

As you can see from the function name, you can replace the string when you replace it, no longer need to write the re or wrap the function

const str = 'abbbbbbba'

const replaceResult = str.replace('a', 'c')
const replaceAllResult = str.replaceAll('a','c')

console.log(replaceResult) // 'cbbbbbbba'
console.log(replaceAllResult) // 'cbbbbbbbc'
Copy the code

The new logic assignment operator | | =,?????? && = =

When writing code, you will always have to check whether the value of a variable exists.

| | = operator

When the value to the left of the operator does not exist, the right-hand value is assigned to the left.

// Normal level
if(!this.currentStudentId) this.currentStudentId = student.id 
/ / simplified
this.currentStudentId = this.currentStudentId || student.id 

/ / | | = operator When there is no value of the left and the right values assigned to the left.
let testNull = null
let testBool = false
let testNumber = 10 

testNull ||= 3 // => testNull = 3
testBool ||= 4 // => testBool = 4
testNumber ||= 5 //=> testNumber = 10
Copy the code
&& = operator

When a value to the left of the operator exists, the right-hand value is assigned to the left.

// Normal level
if(this.currentStudentId) this.currentStudentId = student.id 
/ / simplified
this.currentStudentId = this.currentStudentId && student.id 

/ / | | = operator When there is no value of the left and the right value attached to the left.
let testNull = null
let testBool = false
let testNumber = 10 

testNull &&= 3 // => testNull = null
testBool &&= 4 // => testBool = false
testNumber &&= 5 //=> testNumber = 5
Copy the code
?? = operator

When the value to the left of the operator is null, the right-hand value is assigned to the left.

// Normal level
if(isNull(this.currentStudentId)) this.currentStudentId = student.id 

/ / | | = operator When there is no value of the left and the right value attached to the left.
let testNull = null
let testBool = false
let testNumber = 10testNull ?? =3 // => testNull = 3testBool ?? =4 // => testBool = falsetestNumber ?? =5 //=> testNumber = 10
Copy the code

Promise.any()

Prmoise.any() is somewhat similar to all() in that it returns the first resolve and fullfilled Promise.

Promises are rejected, return an error message.

async getFirstResolve() {
    const promiseArr = [
        Promise.resolve(1),
        Promise.resolve(2),]const result1 = await Promise.any(promiseArr)
    console.log(result1) // result1 => 1
    
    // When the first promise is reject, the first resolve returned becomes the second
    const promiseArrHasReject = [
        Promise.reject(1),
        Promise.resolve(2),]const result2 = await Promise.any(promiseArrHasReject)
    console.log(result2) // result => 2
    
     const promiseArrHasAllReject = [
        Promise.reject(1),
        Promise.reject(2),]try {
           const result3 = await Promise.any(promiseArrHasAllReject)
    } catch (error) {
       console.error(error); 
       // AggregateError: All promises were rejected}}Copy the code

WeakRefs

WeakSet and WeakMap’s smaller sibling, uses weak reference objects that do not block GC and can be removed using WeakRef.prototype.deref before GC.

_ the delimiter

Split the value of type Number to make it easier to understand.

const money = 100 _000_000
// 100,000,000 instead of counting zeros, you can see that you have a billion.
Copy the code

The last

In vain.