iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 25
0
Modern Web

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

ES2020(ES11) - string、bigint、Promise、globalThis

本系列文章經過重新編排和擴充,已出書為ECMAScript關鍵30天。原始文章因當時準備時程緊迫,多少有些許錯誤。為了避免造成讀者的困擾,以及配合書籍的內容規劃,將會陸續更新本系列文章。
本篇文章在 2021/11/8 已更新。

string

String.prototype.matchAll(regexp)

字串物件的matchAll,可以傳入正規表達式regexp,並且以迭代器的資料結構,回傳所有適配regexp的子字串。

const allMatched = "One Punch Man!".matchAll(/[a-h]/g);
for (let v of allMatched) {
  console.log(v);
}

/*
["e", index: 2, input: "One Punch Man!", groups: undefined]
["c", index: 7, input: "One Punch Man!", groups: undefined]
["h", index: 8, input: "One Punch Man!", groups: undefined]
["a", index: 11, input: "One Punch Man!", groups: undefined]
*/

bigint

Javascript 在數值範圍上一直以來都有大小限制。以 Number.MIN_SAFE_INTEGER(-(2^53-1))和 Number.MAX_SAFE_INTEGER(2^53-1)來表示安全範圍內的整數。如果超過這範圍的計算,就會失去準確性。

let n = Number.MAX_SAFE_INTEGER; // 9007199254740991

n = n + 1; // 9007199254740992

n = n + 1; // 再加一就超過安全範圍,數值還是會回傳9007199254740992

9007199254740992 === 9007199254740993; // 這時候就會造成失準,回傳true

因此在 ES2020,有訂定新的內建物件 - BigInt。跟Number一樣可以使用運算符,並且可以進行龐大整數的運算。

建立BigInt有兩種方式 -

  1. 在數字後面加上n
    const n = 123n;
    console.log(typeof n); // BigInt;
    
  2. 使用 BigInt(number)宣告變數
    const n = BigInt(123);
    console.log(typeof n); // BigInt;
    

Promise

Promise.allSettled(promises)

Day14的時候,介紹了 Promise 針對一次執行多個 promise 時,兩種靜態方法的提供 -

  1. Promise.all: 全部的 promise 執行結果為 resolved 時,才會執行 then 裡的 callback 函式,否則會跳到 catch 進行意外處理
  2. Promise.race: 只要有其中一個 promise 執行完成且 resolved 的狀態,就執行 then 裡的 callback 函式,如果都回傳 rejected 的話,會跳到 catch 進行意外處理

而新增的靜態方法 - Promise.allSettled的情境是,只要所有的 promise 執行結束,並回傳 resolvedrejected的話,就執行 then 裡的 callback 函式。

const promiseArr = [Promise.resolve(1), Promise.reject(new Error("ooh!"))];
Promise.allSettled(promiseArr).then((results) => console.log(results));

/*
(2) [{…}, {…}]
0: {status: "fulfilled", value: 1}
1: {reason: Error: ooh! , status: "rejected"}
*/

globalThis

在 Javascript 中,不同的執行環境,都有對應的全域this對象。像是 -

  • window:瀏覽器特定
  • self:Web Workers 和瀏覽器特定,NodeJS 無法使用
  • global:NodeJS 特定

如果需要開發一個程式是需要運行在多種執行環境的話,並且要判斷全域this是誰的話,通常會再寫以下像getGlobalThis的函式 -

const getGlobalThis = () => {
  if (typeof self !== "undefined") return self;
  if (typeof window !== "undefined") return window;
  if (typeof global !== "undefined") return global;
  throw new Error("No Global This");
};

在 ES2020 後,在 Javascript 中只要用globalThis就可以取得當前執行環境的全域this

// in web worker
globalThis === self; // true
globalThis === window; // true

// in node.js
globalThis === global; // true

// in browser
globalThis === window; // true

上一篇
ES2020(ES11) - ESM模組
下一篇
ES2021(ES12) - string、Promise、運算子
系列文
從ES到ESNext - 30天輕鬆掌握ECMAScript30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言