iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 27
0
Modern Web

從ES到ESNext - 30天輕鬆掌握ECMAScript系列 第 27

[Day27] ES2021(ES12) - part 2<未完>

WeakRef & 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);
  });
})();
const registry = new FinalizationRegistry((value) => {
  console.log(value);
});

(function () {
  const obj = {};
  registry.register(obj, "Backbencher");
})();

Intl

Intl.ListFormat

const list = ["Apple", "Orange", "Banana"];
new Intl.ListFormat("en-GB", { style: "long", type: "conjunction" }).format(
  list
);
// "Apple, Orange and Banana"
new Intl.ListFormat("zh-tw", { style: "short", type: "conjunction" }).format(
  list
);
// "Apple、Orange和Banana"

`Intl.DateTimeFormat

let o = new Intl.DateTimeFormat("en", {
  timeStyle: "short",
});
console.log(o.format(Date.now())); // "13:31"
let o = new Intl.DateTimeFormat("en", {
  dateStyle: "short",
});
console.log(o.format(Date.now())); // "21.03.2012"
let o = new Intl.DateTimeFormat("en", {
  timeStyle: "medium",
  dateStyle: "short",
});
console.log(o.format(Date.now())); // "21.03.2012, 13:31"

參考資源


上一篇
[Day26] ES2021(ES12) - part 1<未完>
下一篇
[Day28] Typescript (tsconfig.json)
系列文
從ES到ESNext - 30天輕鬆掌握ECMAScript30

尚未有邦友留言

立即登入留言