ES9新特性
1、对象的剩余参数与扩展运算符
let obj = {
name: "twj",
age: 100,
location: "changsha"
}
let { name, ...other } = obj
console.log(name, other)
function test(name, ...other) {
console.log(name, other)
}
test(obj)
let obj1 = {
name: "twj",
location: "changsha"
}
let obj2 = {
name: "xiaoming",
age: 100
}
let obj3 = { ...obj1, ...obj2 }
console.log(obj3)
let obj5 = {
name: "twj",
age: 200
}
let obj6 = { ...obj5 }
console.log(obj6)
2、正则表达式命名捕获组
let str = "今天是2023-04-12"
let reg = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/
let { year, month, day } = reg.exec(str).groups
console.log(year, month, day)
3、Promise.finally()
function ajax() {
return new Promise((resolve, reject) => {
resolve("data-111")
})
}
ajax().then(data => {
console.log(data)
}).catch(err => {
console.log(err)
}).finally(() => {
console.log("finally")
})
4、异步遍历器
function timer(t) {
return new Promise(resolve => {
setTimeout(() => {
resolve("data-" + t)
}, t)
})
}
async function* gen() {
yield timer(1000)
yield timer(2000)
yield timer(3000)
}
async function test() {
let g = gen()
let arr = [g.next(), g.next(), g.next()]
for await (let item of arr) {
console.log("start-", Date.now())
console.log(item)
console.log("end-", Date.now())
}
}
test()