• ES6的symbol及es2021


    1.es6 symbol唯一性

    为对象添加新的方法,防止覆盖原方法,所以需要一个独一无二的数据类型symbol

    a>Symbol('a')参数a作为一种修饰,用去区分

    b>转为字符串和布尔值 String(Symbol()) / Boolean(Symbol())

    c>作为对象的属性时,三种书写方式:

    let mySymbol = Symbol();

    c-1>let a = {}; a[mySymbol] = 'Hello!';

    c-2>let a = { [mySymbol]: 'Hello!' };

    c-3>let a = {}; Object.defineProperty(a, mySymbol, { value: 'Hello!' });

    d>对象属性的遍历

    let info=Object.getOwnPropertySymbols(a);

    for (const val of info) {

        console.log(a[val]);

    };//‘Hello!

    e>Symbol.for()Symbol.keyFor()

    Symbol.for('a')的创建之前会在全局中寻找有没有用Symbol.for()的方式,如果有则不重复创建,直接用已创建的(已登记的)。而Symbol('a')的创建不会去检索全局,直接创建一个新的Symbol类型。

    重新使用同一个 Symbol 值’时Symbol.for():

    let s1 = Symbol.for('foo'); let s2 = Symbol.for('foo'); s1 === s2 // true

    symbol.keyFor()方法返回一个已登记的 Symbol 类型值的key:

    let s1 = Symbol.for("foo"); Symbol.keyFor(s1) // "foo"

    let s2 = Symbol("foo"); Symbol.keyFor(s2) // undefined 已经创建过了,所以是undefined

    2.es2021新特性es12

    a>replaceAll()

    // 用 dogs 替换所有的“cats”:
    str = str.replaceAll('cats', 'dogs');或者str = str.replace(/cats/g, 'dogs');

    b>数字分割符

    const num = 3_685_134_689;//只是在视觉上有一些帮助,对数值本身没有任何影响。

    c>逻辑赋值运算符

    // 逻辑与赋值运算符 (&&=)  x &&= y
    // 以上代码相当于:x = x && d
    // 或者:
    if (x) {
      x = y
    }

    // 逻辑或赋值运算符 (||=):x ||= y
    // 相当于:x = x || y
    // 或者:
    if (!x) {
      x = y
    }

    // 空值合并赋值运算符 (??=):x ??= y
    // 相当于:x = x ?? y
    // 或者:
    if (x == null || x == undefined) {
        x = y
    }

    3.Promise.any()

    Promise.any() 方法接受多个 promise,并在完成其中任何一个的情况下返回 promise。其返回的是 Promise.any() 完成的第一个 promise。如果所有 promise 均被拒绝,则 Promise.any() 将返回 AggregateError,其中包含拒绝的原因。

    (async function() {
      // Await the result of Promise.any():
      const result = await Promise.any([promise1, promise2, promise3])
      console.log(result)
      // Output:
      // 'promise1 is resolved.', 'promise2 is resolved.' or 'promise3 is resolved.'
    })();

    (async function() {
      // Use try...catch to catch the AggregateError:
      try {
        // Await the result of Promise.any():
        const result = await Promise.any([promise1, promise2])
      }

      catch (err) {
        console.log(err.errors)
        // Output:
        // [ 'promise1 was rejected.', 'promise2 was rejected.' ]
      }
    })();

  • 相关阅读:
    Python文件操作
    低代码常见场景【上】|如何解决业务问题
    自然语言处理 微调ChatGLM-6B大模型
    Linux C文件操作
    SecureCRT之Xmodem操作步骤
    快用云科阎志涛:现代数据栈中的数据建模
    JAVASE零基础到高级教程
    致敬最美逆行者网页设计作品 大学生抗疫感动专题网页设计作业模板 疫情感动人物静态HTML网页模板下载
    【vscode】远程云主机的报错
    SpringMvc第四战-【SpringMvc文件上传,下载】
  • 原文地址:https://blog.csdn.net/ANNENBERG/article/details/116429190