关于前端:ES2021新特性

一、 String.prototype.replaceAll

等价于replace正则中的全局匹配g

'hello world'.replace(/o/g, '_'); // hell_ w_rld

'hello world'.replaceAll('o', '_'); // hell_ w_rld

二、 数字宰割符(_)

数字两头能够增加一个‘_’减少可读性,开端和结尾不能应用,不能间断应用多个,然而能够在多处应用

Number(1__000) 有效
Number(100_00) 正确
Number(100_00_00) 正确
Number('1_1') // 只能是数字两头,字符串有效

const num = 1_1
num * 10 => 110

三、 逻辑运算符

增加三种运算符:??=、&&=、||=

let test = undefined // 或者null
test = test ?? 'defaultValue' // 示意的意思是当test的值为null或者undefined时,test值为defaultValue
test = test && 'defaultValue' // 
test = test || 'defaultValue'

新运算符
test ??= 'defaultValue'
test &&= 'defaultValue'
test ||= 'defaultValue'

四、 Promise.any

接管Promise数组作为参数,返回合成的Promise,只有给定的Promise中有一个胜利, 那么就以这个后果作为返回值

const promises = [
    ajax('1.com').then(),
    ajax('2.com').then(),
    ajax('3.com').then()
]
async ....
try{
    const ret = await Promise.any(promises)
    console.log('ret',ret)
}
catch(error) {
    console.log('error',error)
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理