es10
字符串的trimStart()和trimEnd()
trimStart():去除字符串左側(cè)的空格,trimEnd()去除字符串右側(cè)的空格
const str = ` hello `
console.log(str.trimStart())//hello
console.log(str.trimEnd())// hello
數(shù)組擴展方法:flat()扣墩、flatMap()
//flat():把多維數(shù)組降到一維數(shù)組
const arr = [1,2,[3,4,[5,6]]]
console.log(arr.flat())//[1, 2, 3, 4, Array(2)]
console.log(arr.flat(2))//參數(shù)2表示深度,默認值是1
//[1, 2, 3, 4, 5, 6]
//flatMap:如果數(shù)組的map()返回的是二維數(shù)組,則可以把這個二維數(shù)組降到一維數(shù)組濒持。
//最后的結(jié)果與直接使用map()一致
const arr = [1,2,3,4,5]
let newArr = arr.flatMap(item=>[item*10])
console.log(newArr)//[10, 20, 30, 40, 50]
es11
私有屬性
class Person{
//公有屬性
name
//私有屬性
#age
#weight
constructor(name,age,weight){
this.name = name
this.#age = age
this.#weight = weight
}
}
let per = new Person('楊洋',18,'150斤')
console.log(per.name)
console.log(per.#age)//報錯:私有屬性不能在類的外部訪問官套,只能在類的內(nèi)部使用
console.log(per.#weight)
Promise.allSettled([promise對象1,promise對象2,...])
返回的結(jié)果也是個promise對象惋嚎,并且是resolved狀態(tài)(成功)的對象。對象返回的值是個數(shù)組,數(shù)組元素包含了每個promise對象的狀態(tài)和他們的返回值结榄。
const p1 = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve('數(shù)據(jù)1')
},500)
})
const p2 = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve('數(shù)據(jù)2')
},500)
})
let res = Promise.allSettled([p1,p2])
console.log(res)
String.prototype.matchAll()
返回迭代器视哑,迭代器內(nèi)包含正則批量匹配的結(jié)果。
在es9中想匹配所有符合要求的文本跪呈,需要用到while循環(huán)和reg.exec(),
使用 matchAll 误阻,就可以不必使用 while 循環(huán)加 exec 方式(且正則表達式需使用 /g 標志)寻定。
使用 matchAll 會得到一個迭代器的返回值黍氮,配合 for...of, array spread, 或者 Array.from() 可以更方便實現(xiàn)功能。
const str = 'table football, foosball'
const reg = /foo[a-z]*/g
let result = str.matchAll(reg) //迭代器
//for(let v of result){
// console.log(v)
//}
let arr = [...result]//返回一個二維數(shù)組
console.log(arr)//[Array(1), Array(1)]
可選鏈操作符:?.
當對象作為參數(shù),層級比較深的時候本股,要獲取內(nèi)部的屬性值會比較麻煩案站,使用?.可以簡化獲取對象屬性值的判斷條件蟆盐。
const obj = {
db:{
name:'XX數(shù)據(jù)庫',
publisheTime:'2020/3/3'
}
}
let main = (options) =>{
console.log(options && options.db && options.db.name)//XX數(shù)據(jù)庫
}
main(obj)
//上面這種方式需要先判斷有沒有傳入?yún)?shù)痹愚,參數(shù)中有沒有db屬性城瞎,最后才能拿到db的屬性值
let main1 = (options) =>{
//?.的意思:先判斷前面options是否存在,存在則往后判斷是否存在db屬性,存在則可以拿到db屬性的值
console.log(options?.db?.name)//XX數(shù)據(jù)庫
}
main1(obj)
BigInt數(shù)據(jù)類型
//BigInt數(shù)據(jù)類型:表示比Number數(shù)據(jù)類型支持的范圍更大的整數(shù)值。使用BigInt,整數(shù)溢出將不再是問題。
//js中Number類型所能支持的整數(shù)范圍是(-9007199254740991,9007199254740991)
console.log(Number.MAX_SAFE_INTEGER) //9007199254740991
//當某個整數(shù)超出Number.MAX_SAFE_INTEGER時网杆,會被四舍五入,造成整數(shù)精度損失
console.log(9007199254740995)
console.log(9007199254740997)
//表示方式
console.log(BigInt(123))
//應(yīng)用:大數(shù)值的運算
let max = Number.MAX_SAFE_INTEGER
console.log(BigInt(max) + BigInt(1))//9007199254740992n
console.log(BigInt(max) + BigInt(3))//9007199254740994n
console.log(BigInt(max) + BigInt(7))//9007199254740998n