zoukankan      html  css  js  c++  java
  • ES11(ES2020)学习笔记

     
    1.Promise.all与Promise.allSettled
    Promise.allSettled() 更适合:
    • 彼此不依赖,其中任何一个被 reject ,对其它都没有影响
    • 期望知道每个 promise 的执行结果
    Promise.all() 更适合:
    • 彼此相互依赖,其中任何一个被 reject ,其它都失去了实际价值
    2.链合并运算符
    A. 链式判断运算符
    原本写法:const userName = (list && list.info && list.info.base && list.info.base.userName) || 'userName';
    当前可用:const userName = list?.info?.base?.userName || 'userName';
    B.链合并运算符
    在调用的时候判断左侧的对象是否为null或undefined。如果是的,就不再往下运算,而是返回undefined。
    三种用法:
    • obj?.prop // 对象属性
    • obj?.[expr] // 同上
    • func?.(...args) // 函数或对象方法的调用
    3.Null判断运算符(??)
       属性值为null或undefined时,指定默认值
    eg.    (a && b) ?? c
    4.import()
    一种使用动态说明符异步导入模块的语法
    1. 按需加载(比如点击时加载某个文件或者模块)
    2. 条件加载(比如if判断模块中)
    3. 动态的模块路径(比如模块路径是实时生成)
    5.export * as ns from 'module'
    export { foo, bar } from 'my_module';
    // 可以简单理解为
    import { foo, bar } from 'my_module';
    export { foo, bar };
    6.BigInt
    一个用于处理任意精度整数的新数字基元
    const a = 2172141653n;const b = 15346349309n;
    // BigInt 可以保持精度
    a * b // 33334444555566667777n
     
    // 普通整数无法保持精度Number(a) * Number(b) // String.prototype.matchAll()33334444555566670000
    7.String.prototype.matchAll()
     
    可一次性取出所有匹配,但是返回的是一个遍历器(Iterator),而非数组。可以用for of循环取出。
    相对于返回数组,返回遍历器的好处在于,如果遍历结果是一个很大的数组,那么遍历器比较节省资源。
    遍历器转换成数组,也是比较方便,可以使用...运算符和Array.from()就可以了。
    let regex = /t(e)(st(d?))/g;
    let string = 'test1test2test3';
    [...string.matchAll(regex)];
    Array.from(string.matchAll(regex))
    8.globalThis
    一种在不同环境中获取顶层对象的通用方式
    不同环境中获取全局对象的方法不同
    • 浏览器里面,顶层对象是window,但 Node 和 Web Worker 没有window。
    • 浏览器和 Web Worker 里面,self也指向顶层对象,但是 Node 没有self。
    • Node 里面,顶层对象是global,但其他环境都不支持
    为了实现在不同环境中取到顶层对象,可采用下面三元表达式的方法实现。
    // 方法一(typeof window !== 'undefined'
       ? window
       : (typeof process === 'object' &&
          typeof require === 'function' &&
          typeof global === 'object')
         ? global
         : this);
     
     
    // 方法二var getGlobal = function () {
      if (typeof self !== 'undefined') { return self; }
      if (typeof window !== 'undefined') { return window; }
      if (typeof global !== 'undefined') { return global; }
      throw new Error('unable to locate global object');};
     
  • 相关阅读:
    [Linux] Nginx服务下统计网站的QPS
    [Go] go等待读取最后一行的数据内容
    [Go] Golang中的面向对象
    [Linux] 常见的并发模型
    [PHP] pmap可以查看进程占用内存的详细情况
    [PHP] 解决php中上传大文件的错误
    [PHP] 循环查看php-fpm的内存占用情况
    [Go] go中的goto语句跳到指定标签
    Java抽象类(Abstract Class)与接口(Interface)区别
    Java访问级别修饰符
  • 原文地址:https://www.cnblogs.com/july-Vivian/p/15347033.html
Copyright © 2011-2022 走看看