ES9 (ES2018)

Asynchronous Iteration

异步迭代器,在循环中使用异步。

示例:

  async function process(array) {
    for await (let i of array) {
      doSomething(i);
    }
  }

Promise.finally()

补充ES6中Promise的功能。

无论代码执行.then()还是执行.catch()都会执行.finally()

示例:

  function doSomething() {
    doSomething1()
    .then(doSomething2)
    .then(doSomething3)
    .catch(err => {
      console.log(err);
    })
    .finally(() => {
      // finish here!
    });
  }

Rest/Spread 属性

对ES6中Array的展开符 ... 应用到对象中。

示例:

  const myObject = {
    a: 1,
    b: 2,
    c: 3
  };

  const { a, ...x } = myObject;
  // a = 1
  // x = { b: 2, c: 3 }

或者还可以使用到函数的参数中,作为解构。

  restParam({
    a: 1,
    b: 2,
    c: 3
  });

  function restParam({ a, ...x }) {
    // a = 1
    // x = { b: 2, c: 3 }
  }

还可以用作多个对象的合并。

  const obj1 = { a: 1, b: 2, c: 3 };
  const obj2 = { ...obj1, z: 26 };
  // obj2 is { a: 1, b: 2, c: 3, z: 26 }

还有一个很重要的用途就是对象的深拷贝。类似 Object.assgin()

  const obj1 = { a: 1, b: 2, c: 3 };
  const obj2 = obj2;
  const obj3 = {...obj1};
  //obj1 === obj2  is true
  //obj1 === obj3  is false

正则表达式

  • Regular Expression Named Capture Groups

  • 反向断言(lookbehind)

  • dotAll模式

  • Unicode 转义

  • 非转义序列的模板字符串

results matching ""

    No results matching ""