https://www.html.cn/archives/10441

es6

箭头函数

Let/Const

Proxy

解构

模板字符串

Class

Promise
  • 用于异步计算
  • 将异步操作队列化
  • 解决回调地狱
    生成器

es7

Array.prototype.includes

求幂运算 ** ,等价于Math.pow(x,y)
  1. Math.pow4,2)== 4 ** 2

es8

字符串填充padStart,padEnd

Object.values

Object.entries

Object.getOwnPropertyDescriptors()

函数参数列表和调用中的尾随逗号
  1. const doSomething = (var1, var2,) => {
  2. //...
  3. }
  4. doSomething('test2', 'test2',)

Async Functions (异步函数)
  1. function doSomethingAsync() {
  2. return new Promise((resolve) => {
  3. setTimeout(() => resolve('I did something'), 3000)
  4. })
  5. }
  6. async function doSomething() {
  7. console.log(await doSomethingAsync())
  8. }
  9. console.log('Before')
  10. doSomething()
  11. console.log('After')

共享内存 和 Atomics
  • WebWorkers 用于在浏览器中创建多线程程序

es11

空值合并运算符:??
  1. const name0 = undefined;
  2. console.log(name0 ?? 'default') // default
  3. const name1 = null;
  4. console.log(name1 ?? 'default') // default
  5. const name2 = 0;
  6. console.log(name2 ?? 'default') // 0
  7. const name3 = false;
  8. console.log(name3 ?? 'default') // false

可选链操作符:.?
  1. a?.[x]
  2. // 等同于
  3. a == null ? undefined : a[x]
  4. a?.b()
  5. // 等同于
  6. a == null ? undefined : a.b()
  7. a?.()
  8. // 等同于
  9. a == null ? undefined : a()

es12

String.prototype.replaceAll()
  1. let string = 'hello world, hello ES12'
  2. string.replace(/hello/g,'hi') // hi world, hi ES12
  3. string.replaceAll('hello','hi') // hi world, hi ES12
  4. let string = 'hello world, hello ES12'
  5. string.replaceAll(/hello/,'hi')
  6. // Uncaught TypeError: String.prototype.replaceAll called with a non-global

数字分隔符
  1. const money = 1_000_000_000
  2. //等价于
  3. const money = 1000000000

Promise.any()

它接收一个 Promise 可迭代对象(例如数组),只要其中的一个promise成功,就返回那个已经成功的promise

  1. const promises = [
  2. Promise.reject('ERROR A'),
  3. Promise.reject('ERROR B'),
  4. Promise.resolve('result'),
  5. ]
  6. Promise.any(promises).then((value) => {
  7. console.log('value: ', value)
  8. }).catch((err) => {
  9. console.log('err: ', err)
  10. })
  11. // 输出结果:value: result

如果传入的所有promises都失败:

  1. const promises = [
  2. Promise.reject('ERROR A'),
  3. Promise.reject('ERROR B'),
  4. Promise.reject('ERROR C'),
  5. ]
  6. Promise.any(promises).then((value) => {
  7. console.log('value:', value)
  8. }).catch((err) => {
  9. console.log('err:', err)
  10. console.log(err.message)
  11. console.log(err.name)
  12. console.log(err.errors)
  13. })
  14. // 输出结果:
  15. errAggregateError: All promises were rejected
  16. All promises were rejected
  17. AggregateError
  18. ["ERROR A", "ERROR B", "ERROR C"]