跳至主要内容

BigInt:JavaScript 中的任意精度整數

· 閱讀時間約 9 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

BigInt 是 JavaScript 中一種新的數值型原始類型,能夠表示具有任意精度的整數。通過 BigInt,您可以安全地存儲並操作超出數值類型安全整數範圍的大整數。本文通過一些使用案例,並將 BigInt 與 JavaScript 中的 Number 進行比較,來解釋 Chrome 67 中的新功能。

可選擇 `catch` 綁定

· 閱讀時間約 1 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

try 語句的 catch 子句以往需要一個綁定:

try {
doSomethingThatMightThrow();
} catch (exception) {
// ^^^^^^^^^
// 我們必須命名這個綁定,縱使我們不使用它!
handleException();
}

在 ES2019,catch 現在可以在沒有綁定的情況下使用。如果您在處理異常的代碼中不需要 exception 對象時,這很實用。

try {
doSomethingThatMightThrow();
} catch { // → 無綁定!
handleException();
}

可選擇的 catch 綁定支持

`String.prototype.trimStart` 和 `String.prototype.trimEnd`

· 閱讀時間約 1 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

ES2019 引入了 String.prototype.trimStart()String.prototype.trimEnd():

const string = '  hello world  ';
string.trimStart();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'

此功能之前可以通過非標準的 trimLeft()trimRight() 方法實現,這些方法仍然作為新方法的別名保留,從而保證向後兼容性。

const string = '  hello world  ';
string.trimStart();
// → 'hello world '
string.trimLeft();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trimRight();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'

動態 `import()`

· 閱讀時間約 5 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

動態 import() 引入了一種新的類函數形式的 import,相較於靜態 import 解鎖了新的功能。本文比較了兩者並概述了新功能。

`Promise.prototype.finally`

· 閱讀時間約 2 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

Promise.prototype.finally 讓您可以註冊一個回調函數,在 Promise 處於 處理完成 (即已解決或已拒絕)時被調用。

假設您想要獲取一些資料來顯示在頁面上。此外,您還希望在請求開始時顯示載入的旋轉圖標,而在請求完成時隱藏它。如果出現問題,則改為顯示錯誤訊息。

const fetchAndDisplay = ({ url, element }) => {
showLoadingSpinner();
fetch(url)
.then((response) => response.text())
.then((text) => {
element.textContent = text;
hideLoadingSpinner();
})
.catch((error) => {
element.textContent = error.message;
hideLoadingSpinner();
});
};

`Intl.PluralRules`

· 閱讀時間約 4 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

Iñtërnâtiônàlizætiøn 很難。處理複數是許多看似簡單的問題之一,直到你意識到每種語言都有自己的複數規則。

對於英文複數規則,只有兩種可能的結果。讓我們以“cat”這個詞為例:

  • 1 cat,也就是 'one' 形式,英文中稱為單數
  • 2 cats,但也包括 42 cats、0.5 cats 等,也就是 'other' 形式(唯一的另一種),英文中稱為複數。

全新的 Intl.PluralRules API 告訴你根據給定的數字,某種語言應使用哪種形式。

const pr = new Intl.PluralRules('en-US');
pr.select(0); // 'other' (例如 '0 cats')
pr.select(0.5); // 'other' (例如 '0.5 cats')
pr.select(1); // 'one' (例如 '1 cat')
pr.select(1.5); // 'other' (例如 '0.5 cats')
pr.select(2); // 'other' (例如 '0.5 cats')

物件的剩餘與展開特性

· 閱讀時間約 2 分鐘
Mathias Bynens ([@mathias](https://twitter.com/mathias))

在討論 物件的剩餘與展開特性 之前,我們先回顧一下非常相似的一個功能。

ES2015 陣列的剩餘與展開元素

早在 ECMAScript 2015 引入了用於陣列解構賦值的 剩餘元素 和用於陣列字面值的 展開元素

// 陣列解構賦值中的剩餘元素:
const primes = [2, 3, 5, 7, 11];
const [first, second, ...rest] = primes;
console.log(first); // 2
console.log(second); // 3
console.log(rest); // [5, 7, 11]

// 陣列字面值中的展開元素:
const primesCopy = [first, second, ...rest];
console.log(primesCopy); // [2, 3, 5, 7, 11]

ES2018: 物件的剩餘與展開特性 🆕

那麼有什麼新東西呢?一個提案使得物件字面值也可以使用剩餘與展開特性。

// 物件解構賦值中的剩餘特性:
const person = {
firstName: 'Sebastian',
lastName: 'Markbåge',
country: 'USA',
state: 'CA',
};
const { firstName, lastName, ...rest } = person;
console.log(firstName); // Sebastian
console.log(lastName); // Markbåge
console.log(rest); // { country: 'USA', state: 'CA' }