https://www.html.cn/archives/10441
es6
箭头函数
Let/Const
Proxy
解构
模板字符串
Class
Promise
es7
Array.prototype.includes
求幂运算 **
,等价于Math.pow(x,y)
Math.pow(4,2)== 4 ** 2
es8
字符串填充padStart,padEnd
Object.values
Object.entries
Object.getOwnPropertyDescriptors()
函数参数列表和调用中的尾随逗号
const doSomething = (var1, var2,) => {
//...
}
doSomething('test2', 'test2',)
Async Functions (异步函数)
function doSomethingAsync() {
return new Promise((resolve) => {
setTimeout(() => resolve('I did something'), 3000)
})
}
async function doSomething() {
console.log(await doSomethingAsync())
}
console.log('Before')
doSomething()
console.log('After')
共享内存 和 Atomics
- WebWorkers 用于在浏览器中创建多线程程序
es11
空值合并运算符:??
const name0 = undefined;
console.log(name0 ?? 'default') // default
const name1 = null;
console.log(name1 ?? 'default') // default
const name2 = 0;
console.log(name2 ?? 'default') // 0
const name3 = false;
console.log(name3 ?? 'default') // false
可选链操作符:.?
a?.[x]
// 等同于
a == null ? undefined : a[x]
a?.b()
// 等同于
a == null ? undefined : a.b()
a?.()
// 等同于
a == null ? undefined : a()
es12
String.prototype.replaceAll()
let string = 'hello world, hello ES12'
string.replace(/hello/g,'hi') // hi world, hi ES12
string.replaceAll('hello','hi') // hi world, hi ES12
let string = 'hello world, hello ES12'
string.replaceAll(/hello/,'hi')
// Uncaught TypeError: String.prototype.replaceAll called with a non-global
数字分隔符
const money = 1_000_000_000
//等价于
const money = 1000000000
Promise.any()
它接收一个 Promise
可迭代对象(例如数组),只要其中的一个promise
成功,就返回那个已经成功的promise
:
const promises = [
Promise.reject('ERROR A'),
Promise.reject('ERROR B'),
Promise.resolve('result'),
]
Promise.any(promises).then((value) => {
console.log('value: ', value)
}).catch((err) => {
console.log('err: ', err)
})
// 输出结果:value: result
如果传入的所有promises
都失败:
const promises = [
Promise.reject('ERROR A'),
Promise.reject('ERROR B'),
Promise.reject('ERROR C'),
]
Promise.any(promises).then((value) => {
console.log('value:', value)
}).catch((err) => {
console.log('err:', err)
console.log(err.message)
console.log(err.name)
console.log(err.errors)
})
// 输出结果:
err:AggregateError: All promises were rejected
All promises were rejected
AggregateError
["ERROR A", "ERROR B", "ERROR C"]