JS practical new grammar — Part 2



1.async await 

Make asynchronous requests the same way as synchronous requests
For example:
async () => {
  Let data = await asynchronous request
Data will be printed after the asynchronous request returns, so data will not be undefined as before.



Used in conjunction with try catch, the function that will be executed at the end
For example:
 try {
  Normal logic
}  catch (err) {
  Normal logic遇到到错误进来这里
} finally{
  Enter here after the previous logic is completed



Choose the largest number
For example:
const val = [99,100,-1,3]
console.log(Math.max(...val))  //100
console.log(Math.max(1,23,-1,10))  //23 
Note that it can only be numbers, otherwise Nan will be returned



1. Trimstart() and trimend()

Remove spaces at the beginning or end of the text
For example:
const str =  " 1111 " 
console.log(str.trimStart())  // "1111 "
console.log(str.trimEnd())  // " 1111"
console.log(str.trimStart().trimEnd())  // "1111"



Es8 introduces object Entries, which turns an object into a key value pair (see the previous article for details)
This function instead turns the array into an object.
For example:
const val =[ [a:11] ]?
cosnt obj = Object.fromEntries(val)  // { a:11 }


3.try.. The parameters of catch in catch become optional and no longer necessary




Large integer. The maximum value of the previous number is only 2 ^ 53-1, that is, 9007199254740991. If it is larger, it will be distorted
Bigint has appeared, which solves the problem of distortion. It is easy to use a number and add n
For example:
const val = 90071992547409911111n 
console.log(val)  //90071992547409911111

2. ?? And

?? Null value consolidation Is an optional chain
a ??  B returns the value of a as long as a on the left is not undefined or null. Otherwise, it returns the value of B
a?. B optional chain, only when The latter value will only be taken if it exists, otherwise it will return undefined, which is a good helper to prevent error reporting
For example:
0??1  // 0
'x'??2 //x
undefine? 2 //2
null??3 // 3
For example:
const a={ aa: { b :1 } }
a?.aa?.aaa  //undefine
a?.aa?.b    //1




Specify the text in the replacement text as the new text const STR = "aaaccc" str.replaceall ("a", "1") // 111ccc. Note that replaceall will not affect the original variable. Remember to assign a value if you want to change it


Recommended Today

Super simple method to locate the IP address of the other party with mobile phone

Zero basic hacker, search official account: white hat left first This article is only for the technical discussion of network security. It is forbidden to use it in illegal ways, and it is OK and cherished!   Recently, many fans’ private letters told me that it’s too troublesome to check each other’s physical IP address […]