From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/bigint/asintn/index.html | 73 ++++++ .../global_objects/bigint/asuintn/index.html | 72 ++++++ .../global_objects/bigint/bigint/index.html | 57 +++++ .../reference/global_objects/bigint/index.html | 272 +++++++++++++++++++++ .../bigint/tolocalestring/index.html | 116 +++++++++ .../global_objects/bigint/tostring/index.html | 91 +++++++ .../global_objects/bigint/valueof/index.html | 55 +++++ 7 files changed, 736 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/asintn/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/asuintn/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/bigint/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/tostring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/bigint/valueof/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/bigint') diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/asintn/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/asintn/index.html new file mode 100644 index 0000000000..cab753a464 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/asintn/index.html @@ -0,0 +1,73 @@ +--- +title: BigInt.asIntN() +slug: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN +--- +
{{JSRef}}
+ +

BigInt.asIntN 静态方法将 BigInt 值转换为一个 -2width-1 与 2width-1-1 之间的有符号整数。

+ +
{{EmbedInteractiveExample("pages/js/bigint-asintn.html")}}
+ + + +

语法

+ +
BigInt.asIntN(width, bigint);
+ +

参数

+ +
+
width
+
可存储整数的位数。
+
bigint
+
要存储在指定位数上的整数。
+
+ +

返回值

+ +

bigint 模(modulo) 2width 作为有符号整数的值。

+ +

例子

+ +

保持在64位范围内

+ +

BigInt.asIntN() 方法对于保持在64位(64-bit)算数范围内非常有用。

+ +
const max = 2n ** (64n - 1n) - 1n;
+
+BigInt.asIntN(64, max);
+// ↪ 9223372036854775807n
+
+BigInt.asIntN(64, max + 1n);
+// ↪ -9223372036854775808n
+// negative because of overflow
+
+ +

标准

+ + + + + + + + + + + + +
SpecificationStatus
{{SpecName('ESDraft', '#sec-bigint.asintn', 'BigInt.asIntN()')}}{{Spec2('ESDraft')}}
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.BigInt.asIntN")}}

+ +

请参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/asuintn/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/asuintn/index.html new file mode 100644 index 0000000000..2c813a51ad --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/asuintn/index.html @@ -0,0 +1,72 @@ +--- +title: BigInt.asUintN() +slug: Web/JavaScript/Reference/Global_Objects/BigInt/asUintN +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/asUintN +--- +
{{JSRef}}
+ +

BigInt.asUintN 静态方法将 BigInt 转换为一个 0 和 2width-1 之间的无符号整数。

+ +
{{EmbedInteractiveExample("pages/js/bigint-asuintn.html")}}
+ + + +

语法

+ +
BigInt.asUintN(width, bigint);
+ +

参数

+ +
+
width
+
可存储整数的位数。
+
bigint
+
 要存储在指定位数上的整数。
+
+ +

返回值

+ +

bigint 模(modulo) 2width 作为无符号整数的值。

+ +

例子

+ +

保持在64位范围内

+ +

BigInt.asUintN() 方法对于保持在64位(64-bit)算数范围内非常有用。

+ +
const max = 2n ** 64n - 1n;
+
+BigInt.asUintN(64, max);
+// ↪ 18446744073709551615n
+
+BigInt.asUintN(64, max + 1n);
+// ↪ 0n
+// zero because of overflow
+ +

标准

+ + + + + + + + + + + + +
SpecificationStatus
{{SpecName('ESDraft', '#sec-bigint.asuintn', 'BigInt.asUintN()')}}{{Spec2('ESDraft')}}
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.BigInt.asUintN")}}

+ +

请参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/bigint/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/bigint/index.html new file mode 100644 index 0000000000..7e7a2923f6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/bigint/index.html @@ -0,0 +1,57 @@ +--- +title: BigInt() constructor +slug: Web/JavaScript/Reference/Global_Objects/BigInt/BigInt +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/BigInt +--- +
{{JSRef}}
+ +

 BigInt() 构造函数用来创建 {{jsxref("BigInt")}} 对象。

+ +

语法

+ +
BigInt(value);
+
+ +

参数

+ +
+
value
+
被创建的对象的数值。 可以是字符串或整数。
+
+ +
+

Note: BigInt() 不与 {{JSxRef("Operators/new", "new")}} 运算符一起使用。

+
+ +

例子

+ +
BigInt(123);
+// 123n
+
+ +

规范

+ + + + + + + + + + + + +
SpecificationStatus
{{SpecName('ESDraft', '#sec-bigint-constructor', 'BigInt constructor')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.BigInt.BigInt")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/index.html new file mode 100644 index 0000000000..205b9f9952 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/index.html @@ -0,0 +1,272 @@ +--- +title: BigInt +slug: Web/JavaScript/Reference/Global_Objects/BigInt +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt +--- +

{{JSRef}}

+ +

BigInt 是一种内置对象,它提供了一种方法来表示大于 253 - 1 的整数。这原本是 Javascript中可以用 {{JSxRef("Number")}} 表示的最大数字。BigInt 可以表示任意大的整数。

+ +
+
+ +

描述

+ +

可以用在一个整数字面量后面加 n 的方式定义一个 BigInt ,如:10n,或者调用函数BigInt()

+ +
const theBiggestInt = 9007199254740991n;
+
+const alsoHuge = BigInt(9007199254740991);
+// ↪ 9007199254740991n
+
+const hugeString = BigInt("9007199254740991");
+// ↪ 9007199254740991n
+
+const hugeHex = BigInt("0x1fffffffffffff");
+// ↪ 9007199254740991n
+
+const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
+// ↪ 9007199254740991n
+ +

它在某些方面类似于 {{jsxref("Global_Objects/Number", "Number")}} ,但是也有几个关键的不同点:不能用于 {{jsxref("Global_Objects/Math", "Math")}} 对象中的方法;不能和任何 {{jsxref("Global_Objects/Number", "Number")}} 实例混合运算,两者必须转换成同一种类型。在两种类型来回转换时要小心,因为 BigInt 变量在转换成 {{jsxref("Global_Objects/Number", "Number")}} 变量时可能会丢失精度。

+ +

类型信息

+ +

使用 typeof 测试时, BigInt 对象返回 "bigint" :

+ +
typeof 1n === 'bigint'; // true
+typeof BigInt('1') === 'bigint'; // true
+ +

使用 Object 包装后, BigInt 被认为是一个普通 "object" :

+ +
typeof Object(1n) === 'object'; // true
+ +

运算

+ +

以下操作符可以和 BigInt 一起使用: +、`*`、`-`、`**`、`%` 。除 >>> (无符号右移)之外的 位操作 也可以支持。因为 BigInt 都是有符号的, >>> (无符号右移)不能用于 BigInt为了兼容 asm.js BigInt 不支持单目 (+) 运算符。

+ +
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
+// ↪ 9007199254740991n
+
+const maxPlusOne = previousMaxSafe + 1n;
+// ↪ 9007199254740992n
+
+const theFuture = previousMaxSafe + 2n;
+// ↪ 9007199254740993n, this works now!
+
+const multi = previousMaxSafe * 2n;
+// ↪ 18014398509481982n
+
+const subtr = multi – 10n;
+// ↪ 18014398509481972n
+
+const mod = multi % 10n;
+// ↪ 2n
+
+const bigN = 2n ** 54n;
+// ↪ 18014398509481984n
+
+bigN * -1n
+// ↪ –18014398509481984n
+
+ +

/ 操作符对于整数的运算也没问题。可是因为这些变量是 BigInt 而不是 BigDecimal ,该操作符结果会向零取整,也就是说不会返回小数部分。

+ +
+

当使用 BigInt 时,带小数的运算会被取整。

+
+ +
const expected = 4n / 2n;
+// ↪ 2n
+
+const rounded = 5n / 2n;
+// ↪ 2n, not 2.5n
+
+
+ +

比较

+ +

BigInt 和 {{jsxref("Global_Objects/Number", "Number")}} 不是严格相等的,但是宽松相等的。

+ +
0n === 0
+// ↪ false
+
+0n == 0
+// ↪ true
+ +

{{jsxref("Global_Objects/Number", "Number")}} 和 BigInt 可以进行比较。

+ +
1n < 2
+// ↪ true
+
+2n > 1
+// ↪ true
+
+2 > 2
+// ↪ false
+
+2n > 2
+// ↪ false
+
+2n >= 2
+// ↪ true
+ +

两者也可以混在一个数组内并排序。

+ +
const mixed = [4n, 6, -12n, 10, 4, 0, 0n];
+// ↪  [4n, 6, -12n, 10, 4, 0, 0n]
+
+mixed.sort();
+// ↪ [-12n, 0, 0n, 10, 4n, 4, 6]
+ +

注意被  Object 包装的 BigInts 使用 object 的比较规则进行比较,只用同一个对象在比较时才会相等。

+ +
0n === Object(0n); // false
+Object(0n) === Object(0n); // false
+
+const o = Object(0n);
+o === o // true
+ +

条件

+ +

BigInt 在需要转换成 {{jsxref("Global_Objects/Boolean", "Boolean")}} 的时表现跟 {{jsxref("Global_Objects/Number", "Number")}} 类似:如通过 {{jsxref("Global_Objects/Boolean", "Boolean")}} 函数转换;用于 {{jsxref("Operators/Logical_Operators", "Logical Operators")}}  ||, `&&`, 和 ! 的操作数;或者用于在像 {{jsxref("Statements/if...else", "if statement")}} 这样的条件语句中。

+ +
if (0n) {
+  console.log('Hello from the if!');
+} else {
+  console.log('Hello from the else!');
+}
+
+// ↪ "Hello from the else!"
+
+0n || 12n
+// ↪ 12n
+
+0n && 12n
+// ↪ 0n
+
+Boolean(0n)
+// ↪ false
+
+Boolean(12n)
+// ↪ true
+
+!12n
+// ↪ false
+
+!0n
+// ↪ true
+
+ +

构造器

+ +
+
BigInt()
+
创建{{jsxref("BigInt")}} 对象。
+
+ +

静态方法

+ +
+
{{JSxRef("BigInt.asIntN()")}}
+
将 BigInt 值转换为一个 -2width-1 与 2width-1-1 之间的有符号整数。
+
{{JSxRef("BigInt.asUintN()")}}
+
将一个 BigInt 值转换为 0 与 2width-1 之间的无符号整数。
+
+ +

实例方法

+ +
+
{{JSxRef("BigInt.prototype.toLocaleString()")}}
+
返回此数字的 language-sensitive 形式的字符串。覆盖 {{JSxRef("Object.prototype.toLocaleString()")}}  方法。
+
{{JSxRef("BigInt.prototype.toString()")}}
+
返回以指定基数(base)表示指定数字的字符串。覆盖 {{JSxRef("Object.prototype.toString()")}} 方法。
+
{{JSxRef("BigInt.prototype.valueOf()")}}
+
返回指定对象的基元值。 覆盖 {{JSxRef("Object.prototype.valueOf()")}} 方法。
+
+ +

使用建议

+ +

转化

+ +

由于在 {{JSxRef("Number")}} 与 BigInt 之间进行转换会损失精度,因而建议仅在值可能大于253 时使用 BigInt 类型,并且不在两种类型之间进行相互转换。

+ +

密码学

+ +

由于对 BigInt 的操作不是常数时间的,因而 BigInt 不适合用于密码学

+ +

在 JSON 中使用

+ +

对任何 BigInt 值使用 {{jsxref("JSON.stringify()")}} 都会引发 TypeError,因为默认情况下 BigInt 值不会在 JSON 中序列化。但是,如果需要,可以实现 toJSON 方法:

+ +
BigInt.prototype.toJSON = function() { return this.toString(); }
+ +

JSON.stringify 现在生成如下字符串,而不是抛出异常:

+ +
JSON.stringify(BigInt(1));
+// '"1"'
+ +

例子

+ +

Calculating Primes

+ +
function isPrime(p) {
+  for (let i = 2n; i * i <= p; i++) {
+    if (p % i === 0n) return false;
+  }
+  return true;
+}
+
+// Takes a BigInt as an argument and returns a BigInt
+function nthPrime(nth) {
+  let maybePrime = 2n;
+  let prime = 0n;
+
+  while (nth >= 0n) {
+    if (isPrime(maybePrime)) {
+      nth -= 1n;
+      prime = maybePrime;
+    }
+    maybePrime += 1n;
+  }
+
+  return prime;
+}
+
+nthPrime(20n)
+// ↪ 73n
+ +

标准

+ + + + + + + + + + + + +
标准状态
BigInt第4阶段
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.BigInt")}}

+ +

实施进度

+ +

下表提供了此功能的每日实现状态,因为此功能尚未达到跨浏览器稳定性。数据是通过在 Test262 中运行相关的特性测试生成的,Test262 是 JavaScript 的标准测试套件,在夜间构建,或者是每个浏览器的 JavaScript 引擎的最新版本中运行。

+ +

{{EmbedTest262ReportResultsTable("BigInt")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.html new file mode 100644 index 0000000000..434bceb0d9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.html @@ -0,0 +1,116 @@ +--- +title: BigInt.prototype.toLocaleString() +slug: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString +--- +
{{JSRef}}
+ +

toLocaleString() 方法返回一个字符串,该字符串具有此 BigInt 的 language-sensitive 表达形式。

+ +
{{EmbedInteractiveExample("pages/js/bigint-tolocalestring.html")}}
+ + + +

语法

+ +
bigIntObj.toLocaleString([locales [, options]])
+ +

参数

+ +

locales 和 options 参数可自定义函数的行为,并允许应用程序指定应使用其格式约定的语言。在忽略 locales 和 options 参数的实现中,使用的 locale 和返回的字符串形式完全依赖于实现。

+ +
{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}
+ +

返回值

+ +

具有此 BigInt 的 language-sensitive 表示形式的字符串。

+ +

例子

+ +

Using toLocaleString

+ +

在不指定语言环境的基本用法中,将返回默认语言环境中带默认选项的格式化字符串。

+ +
var bigint = 3500n;
+
+bigint.toLocaleString();
+// Displays "3,500" if in U.S. English locale
+
+ +

Using locales

+ +

这个例子展示了本地化数字格式的一些变体。为了获得应用程序用户界面中使用的语言的格式,请确保使用 locales 参数指定该语言(可能还有一些备用语言):

+ +
var bigint = 123456789123456789n;
+
+// German uses period for thousands
+console.log(bigint.toLocaleString('de-DE'));
+// → 123.456.789.123.456.789
+
+// Arabic in most Arabic speaking countries uses Eastern Arabic digits
+console.log(bigint.toLocaleString('ar-EG'));
+// → ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩
+
+// India uses thousands/lakh/crore separators
+console.log(bigint.toLocaleString('en-IN'));
+// → 1,23,45,67,89,12,34,56,789
+
+// the nu extension key requests a numbering system, e.g. Chinese decimal
+console.log(bigint.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
+// → 一二三,四五六,七八九,一二三,四五六,七八九
+
+// when requesting a language that may not be supported, such as
+// Balinese, include a fallback language, in this case Indonesian
+console.log(bigint.toLocaleString(['ban', 'id']));
+// → 123.456.789.123.456.789
+
+ +

Using options

+ +

toLocaleString 提供的结果可以使用 options 参数进行自定义:

+ +
var bigint = 123456789123456789n;
+
+// request a currency format
+console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
+// → 123.456.789.123.456.789,00 €
+
+// the Japanese yen doesn't use a minor unit
+console.log(bigint.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
+// → ¥123,456,789,123,456,789
+
+// limit to three significant digits
+console.log(bigint.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
+// → 1,23,00,00,00,00,00,00,000
+
+ +

性能

+ +

格式化大量数字时,最好创建 {{jsxref("NumberFormat")}} 对象并使用其 {{jsxref("NumberFormat.format")}} 属性提供的函数。

+ +

标准

+ + + + + + + + + + + + +
SpecificationStatus
BigIntStage 3
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.BigInt.toLocaleString")}}

+ +

请参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/tostring/index.html new file mode 100644 index 0000000000..487fe80ad2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/tostring/index.html @@ -0,0 +1,91 @@ +--- +title: BigInt.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/BigInt/toString +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/toString +--- +
{{JSRef}}
+ +

toString() 方法返回一个字符串,表示指定 {{jsxref("BigInt")}} 对象。 后面的 "n" 不是字符串的一部分。

+ +
{{EmbedInteractiveExample("pages/js/bigint-tostring.html")}}
+ + + +

语法

+ +
bigIntObj.toString([radix])
+ +

参数

+ +
+
radix{{Optional_inline}}
+
可选,介于 2 到 36 之间的整数,指定用于表示数值的基数。
+
+ +

返回值

+ +

表示指定 {{jsxref("BigInt")}} 对象的字符串。

+ +

异常

+ +
+
{{jsxref("RangeError")}}
+
如果 toString() 的基数小于 2 或大于 36, 则抛出 {{jsxref("RangeError")}}。
+
+ +

描述

+ +

{{jsxref("BigInt")}} 对象重写 {{jsxref("Object")}} 对象的 toString() 方法;它不继承 {{jsxref("Object.prototype.toString()")}}。对于 {{jsxref( "BigInt")}} 对象,toString() 方法返回指定基数中对象的字符串表示形式。

+ +

toString() 方法解析其第一个参数,并尝试返回指定基数(base)的字符串表示形式。对于大于 10 的参数,使用字母表中的字母表示大于 9 的数字。例如,对于十六进制数(以16为基数),使用 a 到 f。

+ +

如果未指定基数,则假定首选基数为10。

+ +

如果 bigIntObj 为负,则保留符号。即使基数是 2,情况也是如此;返回的字符串是 bigIntObj 的正二进制表示,前面是一个 - 符号,而不是 bigIntObj 的两个补码。

+ +

例子

+ +

Using toString

+ +
17n.toString();      // '17'
+66n.toString(2);     // '1000010'
+254n.toString(16);   // 'fe'
+-10n.toString(2);    // -1010'
+-0xffn.toString(2);  // '-11111111'
+
+ +

Negative-zero BigInt

+ +

没有负零 BigInt,因为整数中没有负零。-0.0 是一个 IEEE 浮点概念,只出现在JavaScript {{jsxref("Number")}} 类型中。

+ +
(-0n).toString();      // '0'
+BigInt(-0).toString(); // '0'
+ +

标准

+ + + + + + + + + + + + +
SpecificationStatus
{{SpecName('ESDraft', '#sec-bigint.prototype.tostring', 'BigInt.prototype.toString()')}}{{Spec2('ESDraft')}}
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.BigInt.toString")}}

+ +

请参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/bigint/valueof/index.html new file mode 100644 index 0000000000..0487cfbef3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/valueof/index.html @@ -0,0 +1,55 @@ +--- +title: BigInt.prototype.valueOf() +slug: Web/JavaScript/Reference/Global_Objects/BigInt/valueOf +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/valueOf +--- +
{{JSRef}}
+ +

valueOf() 方法返回 {{jsxref("BigInt")}} 对象包装的原始值。

+ +
{{EmbedInteractiveExample("pages/js/bigint-valueof.html")}}
+ + + +

语法

+ +
bigIntObj.valueOf()
+ +

返回值

+ +

表示指定 {{jsxref("BigInt")}} 对象的原始 BigInt 值。

+ +

例子

+ +

Using valueOf

+ +
typeof Object(1n); // object
+typeof Object(1n).valueOf(); // bigint
+
+ +

标准

+ + + + + + + + + + + + +
SpecificationStatus
{{SpecName('ESDraft', '#sec-bigint.prototype.valueof', 'BigInt.prototype.valueOf()')}}{{Spec2('ESDraft')}}
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.BigInt.valueOf")}}

+ +

请参阅

+ + -- cgit v1.2.3-54-g00ecf