From 27e71c467aa249f21b5ed1b5fe97db4aaed42eee Mon Sep 17 00:00:00 2001 From: Mason Xiong Date: Thu, 5 Dec 2019 17:32:00 +1000 Subject: [PATCH 1/5] copy original bigint en version --- 1-js/99-js-misc/05-bigint/article.md | 128 +++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 1-js/99-js-misc/05-bigint/article.md diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md new file mode 100644 index 0000000000..b11af70ef4 --- /dev/null +++ b/1-js/99-js-misc/05-bigint/article.md @@ -0,0 +1,128 @@ +# BigInt + +[recent caniuse="bigint"] + +`BigInt` is a special numeric type that provides support for integers of arbitrary length. + +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. + +```js +const bigint = 1234567890123456789012345678901234567890n; + +const sameBigint = BigInt('1234567890123456789012345678901234567890'); + +const bigintFromNumber = BigInt(10); // same as 10n +``` + +## Math operators + +`BigInt` can mostly be used like a regular number, for example: + +```js run +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. + +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: + +```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 of bigint to number is always silent, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, causing a precision loss. + +````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. + +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. +```` + +## Comparisons + +Comparisons, such as `<`, `>` work with bigints and numbers just fine: + +```js run +alert(2n > 1n); // true + +alert(2n > 1); // true +``` + +As numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`: + +```js run +alert(1 == 1n); // true + +alert(1 === 1n); // false +``` + +## Boolean operations + +When inside `if` or other boolean operations, bigints behave like numbers. + +For instance, in `if`, bigint `0n` is falsy, other values are truthy: + +```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(0n || 2); // 2 (0n is considered falsy) +``` + +## 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. + +For example, division of bigints always returns an integer. + +To emulate such behavior, a polyfill would need to replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance. + +So, there's no well-known good polyfill. + +Although, the other way around is proposed by the developers of [https://github.com/GoogleChromeLabs/jsbi](JSBI) library. + +They suggest to use JSBI library calls instead of native bigints: + +| Operation | native `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)` | +| ... | ... | ... | + +...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them. + +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, closely following the specification, so the code will be "bigint-ready". + +## 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). From 274f29989ca673c96c9c1603e5ab01846a752bb9 Mon Sep 17 00:00:00 2001 From: Mason Xiong Date: Sat, 7 Dec 2019 11:33:37 +1100 Subject: [PATCH 2/5] translation done --- 1-js/99-js-misc/05-bigint/article.md | 96 ++++++++++++++-------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md index b11af70ef4..b96a7fd1d3 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` 函数的方式用字符串,数字等来创建 bigints。 ```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,105 +24,105 @@ 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` 的结果经过四舍五入后,去掉了小数点后面的部分。所有与 bigints 的运算,返回的结果也是 bigints。 -We can't mix bigints and regular numbers: +我们不可以把 bigints 和普通数字类型混在一起: ```js run -alert(1n + 2); // Error: Cannot mix BigInt and other types +alert(1n + 2); // 错误:不可以把 BigInt 和其他类型混用 ``` -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 +// 数字类型转 bigint alert(bigint + BigInt(number)); // 3 -// bigint to number +// bigint 转数字类型 alert(Number(bigint) + number); // 3 ``` -The conversion of bigint to number is always silent, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, causing a precision loss. +bigint 到数字类型的转换总是悄无声息的,但是如果因为这个 bigint 太大而无法适应数字的类型,那么多余的字节将会被截断,从而导致精度损失。 -````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="bigints 不支持一元加法" +一元加法运算符 `+value`,是一个众所周知的,将 `value` 转换成数字类型的方法。 -On bigints it's not supported, to avoid confusion: +在 bigint 中,这是不被支持的。为了避免混淆: ```js run let bigint = 1n; -alert( +bigint ); // error +alert( +bigint ); // 错误 ``` -So we should use `Number()` to convert a bigint to a number. +所以我们应该用 `Number()` 来将一个 bigint 转换成一个数字。 ```` -## Comparisons +## 比较 -Comparisons, such as `<`, `>` work with bigints and numbers just fine: +bigints 和数字类型的比较, 比如 `<`, `>`, 比较起来没有问题: ```js run -alert(2n > 1n); // true +alert(2n > 1n); // 真 -alert(2n > 1); // true +alert(2n > 1); // 真 ``` -As numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`: +因为数字和 bigints 属于不同类型,他们在 `==` (宽松相等)的情况下是相等的,但`===`(严格相等)的情况下不相等: ```js run -alert(1 == 1n); // true +alert(1 == 1n); // 真 -alert(1 === 1n); // false +alert(1 === 1n); // 假 ``` -## Boolean operations +## 布尔运算符 -When inside `if` or other boolean operations, bigints behave like numbers. +当在 `if` 判断中或其他布尔运算时,bigints 表现的像数字类型一样。 -For instance, in `if`, bigint `0n` is falsy, other values are truthy: +打个比方,在 `if` 判断中,bigint `0n` 的判断结果为假,其他值为真: ```js run if (0n) { - // never executes + // 不会被执行 } ``` -Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers: +bigints 和布尔运算符,比如 `||`,`&&` 之类的用法与数字类型类似: ```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 bigints 比较棘手。原因是许多 JavaScript 运算符,比如 `+`,`-` 之类的,在与 bigints 使用的时候表现的与和普通数字类型有所不同。 -For example, division of bigints always returns an integer. +比如说,bigints 的除法总是返回整数。 -To emulate such behavior, a polyfill would need to 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 [https://github.com/GoogleChromeLabs/jsbi](JSBI) library. +不过,[https://github.com/GoogleChromeLabs/jsbi](JSBI) 这个库的开发者们提出了另一种办法。 -They suggest to use JSBI library calls instead of native bigints: +他们建议调用 JSBI 库来替代原生的 bigints: -| Operation | native `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)` | -| ... | ... | ... | +| 运算 | 原生 `BigInt` | JSBI | +| --------------------— | ----------------- | ------------------------- | +| 由数字创建 | `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. +。。。然后,对于那些支持 `BigInt` 的浏览器,用 polyfill (Babel 插件)来将 JSBI 的调用转换成原生的 bigints。 -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, closely following the specification, so the code will be "bigint-ready". +换句话说,这个方法建议我们在写代码时用 JSBI 来替换原生的 bigints。因为 JSBI 在内部像用数字类型一样用 bigints,并且严格遵循规范,所以代码已经是准备好转换成 bigint(bigint-ready)了。 -## 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 docs on BigInt](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt)。 +- [Specification](https://tc39.es/ecma262/#sec-bigint-objects)。 From ac8c7e5b5f7c0ceda9635bb52d55b7dc3c48e9db Mon Sep 17 00:00:00 2001 From: Mason Xiong Date: Sat, 7 Dec 2019 11:59:20 +1100 Subject: [PATCH 3/5] fix table render issue --- 1-js/99-js-misc/05-bigint/article.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md index b96a7fd1d3..b68e767557 100644 --- a/1-js/99-js-misc/05-bigint/article.md +++ b/1-js/99-js-misc/05-bigint/article.md @@ -69,7 +69,7 @@ alert(2n > 1n); // 真 alert(2n > 1); // 真 ``` -因为数字和 bigints 属于不同类型,他们在 `==` (宽松相等)的情况下是相等的,但`===`(严格相等)的情况下不相等: +因为数字和 bigints 属于不同类型,他们在 `==` (宽松相等)的情况下是相等的,但 `===`(严格相等)的情况下不相等: ```js run alert(1 == 1n); // 真 @@ -111,14 +111,14 @@ Polyfilling bigints 比较棘手。原因是许多 JavaScript 运算符,比如 他们建议调用 JSBI 库来替代原生的 bigints: -| 运算 | 原生 `BigInt` | JSBI | -| --------------------— | ----------------- | ------------------------- | -| 由数字创建 | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | -| 加法 | `c = a + b` | `c = JSBI.add(a, b)` | -| 减法 | `c = a - b` | `c = JSBI.subtract(a, b)` | -| ... | ... | ... | +| 运算 | 原生 `BigInt` | JSBI | +| -----------| ----------------- | ------------------------- | +| 由数字创建 | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | +| 加法 | `c = a + b` | `c = JSBI.add(a, b)` | +| 减法 | `c = a - b` | `c = JSBI.subtract(a, b)` | +| ... | ... | ... | -。。。然后,对于那些支持 `BigInt` 的浏览器,用 polyfill (Babel 插件)来将 JSBI 的调用转换成原生的 bigints。 +然后,对于那些支持 `BigInt` 的浏览器,用 polyfill (Babel 插件)来将 JSBI 的调用转换成原生的 bigints。 换句话说,这个方法建议我们在写代码时用 JSBI 来替换原生的 bigints。因为 JSBI 在内部像用数字类型一样用 bigints,并且严格遵循规范,所以代码已经是准备好转换成 bigint(bigint-ready)了。 From 73f532734b5b01178f30ad5c2b5af647740a677f Mon Sep 17 00:00:00 2001 From: Mason Xiong Date: Sat, 7 Dec 2019 12:31:06 +1100 Subject: [PATCH 4/5] use double quotation instead of single --- 1-js/99-js-misc/05-bigint/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md index b68e767557..29315fa6ea 100644 --- a/1-js/99-js-misc/05-bigint/article.md +++ b/1-js/99-js-misc/05-bigint/article.md @@ -9,7 +9,7 @@ ```js const bigint = 1234567890123456789012345678901234567890n; -const sameBigint = BigInt('1234567890123456789012345678901234567890'); +const sameBigint = BigInt("1234567890123456789012345678901234567890"); const bigintFromNumber = BigInt(10); // 等同于 10n ``` From 164e718bb27b5fd619c54cf988c27480644ab1f8 Mon Sep 17 00:00:00 2001 From: Mason Xiong Date: Tue, 10 Dec 2019 20:49:33 +1000 Subject: [PATCH 5/5] changes according to feedback --- 1-js/99-js-misc/05-bigint/article.md | 46 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/1-js/99-js-misc/05-bigint/article.md b/1-js/99-js-misc/05-bigint/article.md index 29315fa6ea..a652dd7cbe 100644 --- a/1-js/99-js-misc/05-bigint/article.md +++ b/1-js/99-js-misc/05-bigint/article.md @@ -4,7 +4,7 @@ `BigInt` 是一种特殊的数字类型,它提供了对任意长度整数的支持。 -可以用在一个整数字面量后面加 `n` 的方式定义一个 bigint,或者通过调用 `BigInt` 函数的方式用字符串,数字等来创建 bigints。 +创建 bigint 的方式有两种:在一个整数字面量后面加 `n` 或者调用 `BigInt` 函数,后者从字符串、数字等生成 bigints。 ```js const bigint = 1234567890123456789012345678901234567890n; @@ -24,31 +24,31 @@ alert(1n + 2n); // 3 alert(5n / 2n); // 2 ``` -请注意:除法 `5/2` 的结果经过四舍五入后,去掉了小数点后面的部分。所有与 bigints 的运算,返回的结果也是 bigints。 +请注意:除法 `5/2` 的结果经过四舍五入后,去掉了小数点后面的部分。bigints 上的所有运算,其返回结果也是 bigints。 我们不可以把 bigints 和普通数字类型混在一起: ```js run -alert(1n + 2); // 错误:不可以把 BigInt 和其他类型混用 +alert(1n + 2); // Error: Cannot mix BigInt and other types ``` -在需要的情况下,我们应该使用 `BigInt()` 或者 `Number()` 对他们进行明确地转换,像这样: +在需要的情况下,我们应该使用 `BigInt()` 或者 `Number()` 对它们进行明确地转换,像这样: ```js run let bigint = 1n; let number = 2; -// 数字类型转 bigint +// 数字转 bigint alert(bigint + BigInt(number)); // 3 -// bigint 转数字类型 +// bigint 转数字 alert(Number(bigint) + number); // 3 ``` -bigint 到数字类型的转换总是悄无声息的,但是如果因为这个 bigint 太大而无法适应数字的类型,那么多余的字节将会被截断,从而导致精度损失。 +bigint 到数字的转换通常都不会报错,但是如果这个 bigint 太大而不适合数字类型,那么多余的字节将会被截断,从而导致精度损失。 ````smart header="bigints 不支持一元加法" -一元加法运算符 `+value`,是一个众所周知的,将 `value` 转换成数字类型的方法。 +一元加法运算符 `+value`,是大家熟知的将 `value` 转换成数字的方法。 在 bigint 中,这是不被支持的。为了避免混淆: ```js run @@ -64,22 +64,22 @@ alert( +bigint ); // 错误 bigints 和数字类型的比较, 比如 `<`, `>`, 比较起来没有问题: ```js run -alert(2n > 1n); // 真 +alert( 2n > 1n ); // true -alert(2n > 1); // 真 +alert( 2n > 1 ); // true ``` -因为数字和 bigints 属于不同类型,他们在 `==` (宽松相等)的情况下是相等的,但 `===`(严格相等)的情况下不相等: +因为数字和 bigints 属于不同类型,它们可能在`==`的情况下相等,但在`===`(严格相等)的情况下不相等: ```js run -alert(1 == 1n); // 真 +alert( 1 == 1n ); // true -alert(1 === 1n); // 假 +alert( 1 === 1n ); // false ``` ## 布尔运算符 -当在 `if` 判断中或其他布尔运算时,bigints 表现的像数字类型一样。 +当在 `if` 判断或其他布尔运算中时,bigints 和数字用法一样。 打个比方,在 `if` 判断中,bigint `0n` 的判断结果为假,其他值为真: @@ -89,25 +89,25 @@ if (0n) { } ``` -bigints 和布尔运算符,比如 `||`,`&&` 之类的用法与数字类型类似: +bigints 和布尔运算符,比如 `||`、`&&` 之类的用法与数字类型类似: ```js run -alert(1n || 2); // 1 (1n 被认为是真) +alert( 1n || 2 ); // 1 (1n 被认为是真) -alert(0n || 2); // 2 (0n 被认为是假) +alert( 0n || 2 ); // 2 (0n 被认为是假) ``` ## Polyfills -Polyfilling bigints 比较棘手。原因是许多 JavaScript 运算符,比如 `+`,`-` 之类的,在与 bigints 使用的时候表现的与和普通数字类型有所不同。 +Polyfilling bigints 比较棘手。原因是许多 JavaScript 运算符,比如 `+`、`-` 之类的,用于 bigints 的时候与用在普通数字类型有所不同。 比如说,bigints 的除法总是返回整数。 -想要模拟这样的行为,一个 polyfill 需要用它的功能来替换所有的这些运算符。但是这样做很麻烦,并且会耗费很多的性能。 +想要模拟这样的行为,polyfill 需要写函数来替换所有这些运算符。但是这样做很麻烦,并且会耗费很多的性能。 -所以,并没有一个好的,众所周知的 polyfill。 +所以,并没有一个众所周知且好用的 polyfill。 -不过,[https://github.com/GoogleChromeLabs/jsbi](JSBI) 这个库的开发者们提出了另一种办法。 +不过,[JSBI](https://github.com/GoogleChromeLabs/jsbi) 这个库的开发者们提出了另一种办法。 他们建议调用 JSBI 库来替代原生的 bigints: @@ -118,11 +118,11 @@ Polyfilling bigints 比较棘手。原因是许多 JavaScript 运算符,比如 | 减法 | `c = a - b` | `c = JSBI.subtract(a, b)` | | ... | ... | ... | -然后,对于那些支持 `BigInt` 的浏览器,用 polyfill (Babel 插件)来将 JSBI 的调用转换成原生的 bigints。 +然后,对于那些支持 `BigInt` 的浏览器,用 polyfill(Babel 插件)来将 JSBI 的调用转换成原生的 bigints。 换句话说,这个方法建议我们在写代码时用 JSBI 来替换原生的 bigints。因为 JSBI 在内部像用数字类型一样用 bigints,并且严格遵循规范,所以代码已经是准备好转换成 bigint(bigint-ready)了。 -## 引用 +## 参考 - [MDN docs on BigInt](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt)。 - [Specification](https://tc39.es/ecma262/#sec-bigint-objects)。