diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md index 72f06d0c7..94c562cc6 100644 --- a/1-js/99-js-misc/05-bigint/article.md +++ b/1-js/99-js-misc/05-bigint/article.md @@ -2,21 +2,21 @@ [recent caniuse="bigint"] -`BigInt` is a special numeric type that provides support for integers of arbitrary length. +`BigInt` 是一種支援任意整數長度的特殊數值類型。 -A bigint is created by appending `n` to the end of an integer literal or by calling the function `BigInt` that creates bigints from strings, numbers etc. +藉由增加 `n` 到整數字面的最後面建立一個大數(bigint)或藉由呼叫 `BigInt` 函式以字串、數字等建立大數。 ```js const bigint = 1234567890123456789012345678901234567890n; const sameBigint = BigInt("1234567890123456789012345678901234567890"); -const bigintFromNumber = BigInt(10); // same as 10n +const bigintFromNumber = BigInt(10); // 等同 10n ``` -## Math operators +## 數學運算子 -`BigInt` can mostly be used like a regular number, for example: +`BigInt` 通常可以像一般數字使用,例如: ```js run alert(1n + 2n); // 3 @@ -24,44 +24,44 @@ alert(1n + 2n); // 3 alert(5n / 2n); // 2 ``` -Please note: the division `5/2` returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints. +請注意:`5/2` 的除法傳回的是往零方向捨去後的結果,不含小數部份。所有在大數上的操作皆傳回大數。 -We can't mix bigints and regular numbers: +我們不能將大數和一般數字混用: ```js run alert(1n + 2); // Error: Cannot mix BigInt and other types ``` -We should explicitly convert them if needed: using either `BigInt()` or `Number()`, like this: +如果需要使用,我們應該明確轉換它們:使用 `BigInt()` 或 `Number()`,像這樣: ```js run let bigint = 1n; let number = 2; -// number to bigint +// 數字轉大數 alert(bigint + BigInt(number)); // 3 -// bigint to number +// 大數轉數字 alert(Number(bigint) + number); // 3 ``` -The conversion operations are always silent, never give errors, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, so we should be careful doing such conversion. +轉換操作總是默許的,不會給出錯誤,但是如果大數數字大太無法適用在數字類型時,多出來的位元將被截斷,所以我們應該謹慎的進行這類的轉換。 -````smart header="The unary plus is not supported on bigints" -The unary plus operator `+value` is a well-known way to convert `value` to a number. +````smart header="大數不支援一元加號" +一元加號運算子 `+value` 眾所皆知,是將 `value` 轉換為數字的方法。 -On bigints it's not supported, to avoid confusion: +為了避免混淆,在大數上是不支援的: ```js run let bigint = 1n; alert( +bigint ); // error ``` -So we should use `Number()` to convert a bigint to a number. +所以我們應該使用 `Number()` 去把大數轉換成數字。 ```` -## Comparisons +## 比較 -Comparisons, such as `<`, `>` work with bigints and numbers just fine: +像是 `<`、`>` 的比較,用在大數和數字間是可以的: ```js run alert( 2n > 1n ); // true @@ -69,7 +69,7 @@ alert( 2n > 1n ); // true alert( 2n > 1 ); // true ``` -Please note though, as numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`: +但是請注意,由於數字跟大數屬於不同類型,因此它們可以等於 `==`,但不能嚴格等於 `===`: ```js run alert( 1 == 1n ); // true @@ -77,54 +77,54 @@ alert( 1 == 1n ); // true alert( 1 === 1n ); // false ``` -## Boolean operations +## 布林值操作 -When inside `if` or other boolean operations, bigints behave like numbers. +在 `if` 中或其他布林值操作,大數行為類似數字。 -For instance, in `if`, bigint `0n` is falsy, other values are truthy: +例如,在 `if` 中,大數 `0n` 是屬於假,其他值是屬於真: ```js run if (0n) { - // never executes + // 永遠不會執行 } ``` -Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers: +布林值運算子,像是 `||` 、 `&&` 等,類似於數字,也可以用在大數上: ```js run -alert( 1n || 2 ); // 1 (1n is considered truthy) +alert( 1n || 2 ); // 1 (1n是屬於真) -alert( 0n || 2 ); // 2 (0n is considered falsy) +alert( 0n || 2 ); // 2 (0n是屬於假) ``` ## Polyfills -Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as `+`, `-` and so on behave differently with bigints compared to regular numbers. +大數的 Polyfilling 是棘手的。原因是許多 JavaScript 運算子,像是 `+`、`-` 等的行為在大數和一般數字是不同的。 -For example, division of bigints always returns a bigint (rounded if necessary). +例如,大數的除法總是傳回大數(必要時會四捨五入)。 -To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance. +要模擬這些行為,這個 polyfill 需要分析程式碼,並使用其函式取代所有運算子。但是這樣很麻煩,而且還會浪費很多效率。 -So, there's no well-known good polyfill. +因此,沒有眾所皆知的好 polyfill。 -Although, the other way around is proposed by the developers of [JSBI](https://github.com/GoogleChromeLabs/jsbi) library. +儘管 [JSBI](https://github.com/GoogleChromeLabs/jsbi) 程式庫的開發者提供了其他解決辦法。 -This library implements big numbers using its own methods. We can use them instead of native bigints: +這個程式庫使用他們自有的方法實做大數。我們可以使用他們替換原生的大數: -| Operation | native `BigInt` | JSBI | +| 操作 | 原生 `BigInt` | JSBI | |-----------|-----------------|------| -| Creation from Number | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | -| Addition | `c = a + b` | `c = JSBI.add(a, b)` | -| Subtraction | `c = a - b` | `c = JSBI.subtract(a, b)` | +| 以數字建立 | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | +| 加法 | `c = a + b` | `c = JSBI.add(a, b)` | +| 減法 | `c = a - b` | `c = JSBI.subtract(a, b)` | | ... | ... | ... | -...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them. +...然後對於支援大數的瀏覽器,使用 polyfill (Babel plugin) 將那些 JSBI 呼叫轉化為原生大數。 -In other words, this approach suggests that we write code in JSBI instead of native bigints. But JSBI works with numbers as with bigints internally, emulates them closely following the specification, so the code will be "bigint-ready". +換句話說,這方式建議我們使用 JSBI 替代原生的大數來寫程式碼。JSBI 在內部將數字作為大數,按照規格接近的模擬,所以程式碼將 "可用於大數"。 -We can use such JSBI code "as is" for engines that don't support bigints and for those that do support - the polyfill will convert the calls to native bigints. +對於不支援大數的引擎,我們可以 "照原樣" 使用 JSBI 程式碼,而對於有支援的引擎 - polyfill 將轉為呼叫原生大數。 -## References +## 參見 -- [MDN docs on BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). -- [Specification](https://tc39.es/ecma262/#sec-bigint-objects). +- [MDN 文件上的 BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) +- [規格](https://tc39.es/ecma262/#sec-bigint-objects)