{singhi}
🏷 tech
ES2021 新特性
🔗 🌙 💬 📅

String replaceAll() Method

const str = "Backbencher sits at the Back";
const newStr = str.replaceAll("Back", "Front");
console.log(newStr); // "Frontbencher sits at the Front"

WeakRef and Finalizers

const callback = () => {
  const aBigObj = new WeakRef({
    name: "Backbencher",
  });
  console.log(aBigObj.deref().name);
};

(async function () {
  await new Promise((resolve) => {
    setTimeout(() => {
      callback(); // Guaranteed to print "Backbencher"
      resolve();
    }, 2000);
  });

  await new Promise((resolve) => {
    setTimeout(() => {
      callback(); // No Gaurantee that "Backbencher" is printed
      resolve();
    }, 5000);
  });
})();

Promise.any() and AggregateError

const p = new Promise((resolve, reject) => reject());

try {
  (async function () {
    const result = await Promise.any([p]);
    console.log(result);
  })();
} catch (error) {
  console.log(error.errors);
}

Logical Assignment Operator

let x = 1;
let y = 2;
x &&= y;
x ||= y;
x ??= y;

Underscores as Numeric Seperator

const billion = 1000_000_000;
console.log(billion); // 1000000000