Skip to content

ES6、7、8、9、10 新特性 #1

@wangyong315

Description

@wangyong315

#es2020

一、可选链操作符(Optional Chaining)

使用场景
可选链可以让我们在查询具有多个层级的对象时,不在需要进行冗余的各种前置校验。
从而避免 Uncaught TypeError: cannot read property

const obj = {}
const age = obj?.person?.age

安全的取值 部分替代lodash.get

二、空位合并操作符(Nullish coalescing Operator)

使用场景
当我们查询某个属性的时候 经常会给没有该属性设置一个默认值 比如下面两种方式

let c = a ? a : b
let c = a || b

弊端:如果a = 0 但是却得不到我们想要的值

const a = null
const y = a ?? 500
// y = 500
const m = 0
const n = m ?? 900
// n = 0

三、Promise.allSettled

优点:解决了Promise.all的一个接口挂掉导致整个all中的接口全部报错的情况
手动模拟实现一下allSellted

const allSettled = promises => Promise.all(
  promises.map(val => Promise.resolve(val)
    .then(
      val => ({ status: 'fulfilled', value: val }),
      err => ({ status: 'rejected', reason: err})
    )
  )
) 

四、String.prototype.matchAll

五、Dynamic import

动态加载

el.onclick = () => {
  import('/modules/my-module.js')
    .then(module => {
      // doing something with the module
    })
    .catch(err => {
      // log err
    })
}

import() 可以用于script脚本中 import(module)函数可以在任何地方调用
返回一个解析为模块对象的 promise
同时支持 await

BigInt

顾名思义 大数 可以表示超过 正负 2^53-1 的数

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions