diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/bigint | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/bigint')
7 files changed, 736 insertions, 0 deletions
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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>BigInt.asIntN</code></strong> 静态方法将 <code>BigInt</code> 值转换为一个 -<code>2<sup>width-1</sup></code> 与 <code>2<sup>width-1</sup>-1</code> 之间的有符号整数。</p> + +<div>{{EmbedInteractiveExample("pages/js/bigint-asintn.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">BigInt.asIntN(<var>width</var>, <var>bigint</var>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>width</code></dt> + <dd>可存储整数的位数。</dd> + <dt><code>bigint</code></dt> + <dd>要存储在指定位数上的整数。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>bigint</code> 模(modulo) 2<sup><code>width</code></sup> 作为有符号整数的值。</p> + +<h2 id="例子">例子</h2> + +<h3 id="保持在64位范围内">保持在64位范围内</h3> + +<p><code>BigInt.asIntN()</code> 方法对于保持在64位(64-bit)算数范围内非常有用。</p> + +<pre class="brush: js">const max = 2n ** (64n - 1n) - 1n; + +BigInt.asIntN(64, max); +// ↪ 9223372036854775807n + +BigInt.asIntN(64, max + 1n); +// ↪ -9223372036854775808n +// negative because of overflow +</pre> + +<h2 id="标准">标准</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint.asintn', 'BigInt.asIntN()')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + + + +<p>{{Compat("javascript.builtins.BigInt.asIntN")}}</p> + +<h2 id="请参阅">请参阅</h2> + +<ul> + <li>{{JSxRef("BigInt")}}</li> + <li>{{JSxRef("BigInt.asUintN()")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>BigInt.asUintN</code></strong> 静态方法将 <code>BigInt</code> 转换为一个 0 和 2<sup>width</sup>-1 之间的无符号整数。</p> + +<div>{{EmbedInteractiveExample("pages/js/bigint-asuintn.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">BigInt.asUintN(<var>width</var>, <var>bigint</var>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>width</code></dt> + <dd>可存储整数的位数。</dd> + <dt><code>bigint</code></dt> + <dd> 要存储在指定位数上的整数。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>bigint</code> 模(modulo) 2<sup><code>width</code></sup> 作为无符号整数的值。</p> + +<h2 id="例子">例子</h2> + +<h3 id="保持在64位范围内">保持在64位范围内</h3> + +<p><code>BigInt.asUintN()</code> 方法对于保持在64位(64-bit)算数范围内非常有用。</p> + +<pre class="brush: js">const max = 2n ** 64n - 1n; + +BigInt.asUintN(64, max); +// ↪ 18446744073709551615n + +BigInt.asUintN(64, max + 1n); +// ↪ 0n +// zero because of overflow</pre> + +<h2 id="标准">标准</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint.asuintn', 'BigInt.asUintN()')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + + + +<p>{{Compat("javascript.builtins.BigInt.asUintN")}}</p> + +<h2 id="请参阅">请参阅</h2> + +<ul> + <li>{{JSxRef("BigInt")}}</li> + <li>{{JSxRef("BigInt.asIntN()")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p> <strong><code>BigInt()</code></strong> 构造函数用来创建 {{jsxref("BigInt")}} 对象。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">BigInt(<em>value</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>被创建的对象的数值。 可以是字符串或整数。</dd> +</dl> + +<div class="blockIndicator note"> +<p><strong>Note</strong>: <code>BigInt()</code> 不与 {{JSxRef("Operators/new", "new")}} 运算符一起使用。</p> +</div> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">BigInt(123); +// 123n +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint-constructor', 'BigInt constructor')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.BigInt.BigInt")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("BigInt")}} class</li> +</ul> 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 +--- +<p>{{JSRef}}</p> + +<p><strong><code>BigInt</code></strong> 是一种内置对象,它提供了一种方法来表示大于 <code>2<sup>53 </sup>- 1</code> 的整数。这原本是 Javascript中可以用 {{JSxRef("Number")}} 表示的最大数字。<strong><code>BigInt</code></strong> 可以表示任意大的整数。</p> + +<dl> +</dl> + +<h2 id="描述">描述</h2> + +<p>可以用在一个整数字面量后面加 <code>n</code> 的方式定义一个 <code>BigInt</code> ,如:<code>10n</code>,或者调用函数<code>BigInt()</code>。</p> + +<pre><code>const theBiggestInt = 9007199254740991n; + +const alsoHuge = BigInt(9007199254740991); +// ↪ 9007199254740991n + +const hugeString = BigInt("9007199254740991"); +// ↪ 9007199254740991n + +const hugeHex = BigInt("0x1fffffffffffff"); +// ↪ 9007199254740991n + +const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111"); +// ↪ 9007199254740991n</code></pre> + +<p>它在某些方面类似于 {{jsxref("Global_Objects/Number", "Number")}} ,但是也有几个关键的不同点:不能用于 {{jsxref("Global_Objects/Math", "Math")}} 对象中的方法;不能和任何 {{jsxref("Global_Objects/Number", "Number")}} 实例混合运算,两者必须转换成同一种类型。在两种类型来回转换时要小心,因为 <code>BigInt</code> 变量在转换成 {{jsxref("Global_Objects/Number", "Number")}} 变量时可能会丢失精度。</p> + +<h3 id="类型信息">类型信息</h3> + +<p>使用 <code>typeof</code> 测试时, <code>BigInt</code> 对象返回 "bigint" :</p> + +<pre><code>typeof 1n === 'bigint'; // true +typeof BigInt('1') === 'bigint'; // true</code></pre> + +<p>使用 <code>Object</code> 包装后, <code>BigInt</code> 被认为是一个普通 "object" :</p> + +<pre><code>typeof Object(1n) === 'object'; // true</code></pre> + +<h3 id="运算">运算</h3> + +<p>以下操作符可以和 <code>BigInt</code> 一起使用: <code>+</code>、`<code>*</code>`、`<code>-</code>`、`<code>**</code>`、`<code>%</code>` 。除 <code>>>></code> (无符号右移)之外的 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators">位操作</a> 也可以支持。因为 <code>BigInt</code> 都是有符号的, <code>>>></code> (无符号右移)不能用于 <code>BigInt</code>。<a href="https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs">为了兼容 asm.js </a>,<code>BigInt</code> 不支持单目 (<code>+</code>) 运算符。</p> + +<pre class="brush: js">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 +</pre> + +<p><code>/</code> 操作符对于整数的运算也没问题。可是因为这些变量是 <code>BigInt</code> 而不是 <code>BigDecimal</code> ,该操作符结果会向零取整,也就是说不会返回小数部分。</p> + +<div class="blockIndicator warning"> +<p>当使用 <code>BigInt</code> 时,带小数的运算会被取整。</p> +</div> + +<pre class="brush: js">const expected = 4n / 2n; +// ↪ 2n + +const rounded = 5n / 2n; +// ↪ 2n, not 2.5n + +</pre> + +<h3 id="比较">比较</h3> + +<p><code>BigInt</code> 和 {{jsxref("Global_Objects/Number", "Number")}} 不是严格相等的,但是宽松相等的。</p> + +<pre class="brush: js">0n === 0 +// ↪ false + +0n == 0 +// ↪ true</pre> + +<p>{{jsxref("Global_Objects/Number", "Number")}} 和 <code>BigInt</code> 可以进行比较。</p> + +<pre class="brush: js">1n < 2 +// ↪ true + +2n > 1 +// ↪ true + +2 > 2 +// ↪ false + +2n > 2 +// ↪ false + +2n >= 2 +// ↪ true</pre> + +<p>两者也可以混在一个数组内并排序。</p> + +<pre class="brush: js">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]</pre> + +<p>注意被 <code>Object</code> 包装的 <code>BigInt</code>s 使用 object 的比较规则进行比较,只用同一个对象在比较时才会相等。</p> + +<pre><code>0n === Object(0n); // false +Object(0n) === Object(0n); // false + +const o = Object(0n); +o === o // true</code></pre> + +<h3 id="条件">条件</h3> + +<p><code>BigInt</code> 在需要转换成 {{jsxref("Global_Objects/Boolean", "Boolean")}} 的时表现跟 {{jsxref("Global_Objects/Number", "Number")}} 类似:如通过 {{jsxref("Global_Objects/Boolean", "Boolean")}} 函数转换;用于 {{jsxref("Operators/Logical_Operators", "Logical Operators")}} <code>||</code>, `<code>&&</code>`, 和 <code>!</code> 的操作数;或者用于在像 {{jsxref("Statements/if...else", "if statement")}} 这样的条件语句中。</p> + +<pre class="brush: js">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 +</pre> + +<h2 id="构造器">构造器</h2> + +<dl> + <dt><code><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt">BigInt()</a></code></dt> + <dd>创建{{jsxref("BigInt")}} 对象。</dd> +</dl> + +<h2 id="静态方法">静态方法</h2> + +<dl> + <dt>{{JSxRef("BigInt.asIntN()")}}</dt> + <dd>将 BigInt 值转换为一个 -2<sup>width-1</sup> 与 2<sup>width-1</sup>-1 之间的有符号整数。</dd> + <dt>{{JSxRef("BigInt.asUintN()")}}</dt> + <dd>将一个 BigInt 值转换为 0 与 2<sup>width</sup>-1 之间的无符号整数。</dd> +</dl> + +<h2 id="实例方法">实例方法</h2> + +<dl> + <dt>{{JSxRef("BigInt.prototype.toLocaleString()")}}</dt> + <dd>返回此数字的 language-sensitive 形式的字符串。覆盖 {{JSxRef("Object.prototype.toLocaleString()")}} 方法。</dd> + <dt>{{JSxRef("BigInt.prototype.toString()")}}</dt> + <dd>返回以指定基数(base)表示指定数字的字符串。覆盖 {{JSxRef("Object.prototype.toString()")}} 方法。</dd> + <dt>{{JSxRef("BigInt.prototype.valueOf()")}}</dt> + <dd>返回指定对象的基元值。 覆盖 {{JSxRef("Object.prototype.valueOf()")}} 方法。</dd> +</dl> + +<h2 id="使用建议">使用建议</h2> + +<h3 id="转化">转化</h3> + +<p>由于在 {{JSxRef("Number")}} 与 <code>BigInt</code> 之间进行转换会损失精度,因而建议仅在值可能大于2<sup>53</sup> 时使用 <code>BigInt</code> 类型,并且不在两种类型之间进行相互转换。</p> + +<h3 id="密码学">密码学</h3> + +<p>由于对 <code>BigInt</code> 的操作不是常数时间的,因而 <code>BigInt</code> <a href="https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html">不适合用于密码学</a>。</p> + +<h3 id="在_JSON_中使用">在 JSON 中使用</h3> + +<p>对任何 <code>BigInt</code> 值使用 {{jsxref("JSON.stringify()")}} 都会引发 <code>TypeError</code>,因为默认情况下 <code>BigInt</code> 值不会在 <code>JSON</code> 中序列化。但是,如果需要,可以实现 <code>toJSON</code> 方法:</p> + +<pre><code>BigInt.prototype.toJSON = function() { return this.toString(); }</code></pre> + +<p><code>JSON.stringify</code> 现在生成如下字符串,而不是抛出异常:</p> + +<pre><code>JSON.stringify(BigInt(1)); +// '"1"'</code></pre> + +<h2 id="例子">例子</h2> + +<h3 id="Calculating_Primes">Calculating Primes</h3> + +<pre class="brush: js">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</pre> + +<h2 id="标准">标准</h2> + +<table> + <tbody> + <tr> + <th scope="col">标准</th> + <th scope="col">状态</th> + </tr> + <tr> + <td><a href="https://tc39.es/proposal-bigint/#sec-bigint-objects">BigInt</a></td> + <td>第4阶段</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.BigInt")}}</p> + +<h3 id="实施进度">实施进度</h3> + +<p>下表提供了此功能的每日实现状态,因为此功能尚未达到跨浏览器稳定性。数据是通过在 Test262 中运行相关的特性测试生成的,<a href="https://github.com/tc39/test262">Test262</a> 是 JavaScript 的标准测试套件,在夜间构建,或者是每个浏览器的 JavaScript 引擎的最新版本中运行。</p> + +<p>{{EmbedTest262ReportResultsTable("BigInt")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{JSxRef("Number")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>toLocaleString()</code></strong> 方法返回一个字符串,该字符串具有此 <code>BigInt</code> 的 language-sensitive 表达形式。</p> + +<div>{{EmbedInteractiveExample("pages/js/bigint-tolocalestring.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>bigIntObj</em>.toLocaleString(</code><code>[locales [, options]])</code></pre> + +<h3 id="参数">参数</h3> + +<p><code>locales</code> 和 <code>options</code> 参数可自定义函数的行为,并允许应用程序指定应使用其格式约定的语言。在忽略 <code>locales</code> 和 <code>options</code> 参数的实现中,使用的 <code>locale</code> 和返回的字符串形式完全依赖于实现。</p> + +<div>{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}</div> + +<h3 id="返回值">返回值</h3> + +<p>具有此 <code>BigInt</code> 的 language-sensitive 表示形式的字符串。</p> + +<h2 id="例子">例子</h2> + +<h3 id="Using_toLocaleString">Using <code>toLocaleString</code></h3> + +<p>在不指定语言环境的基本用法中,将返回默认语言环境中带默认选项的格式化字符串。</p> + +<pre class="brush: js">var bigint = 3500n; + +bigint.toLocaleString(); +// Displays "3,500" if in U.S. English locale +</pre> + +<h3 id="Using_locales">Using <code>locales</code></h3> + +<p>这个例子展示了本地化数字格式的一些变体。为了获得应用程序用户界面中使用的语言的格式,请确保使用 <code>locales</code> 参数指定该语言(可能还有一些备用语言):</p> + +<pre class="brush: js">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 <a href="https://en.wikipedia.org/wiki/Eastern_Arabic_numerals">Eastern Arabic</a> 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 +</pre> + +<h3 id="Using_options">Using <code>options</code></h3> + +<p><code>toLocaleString</code> 提供的结果可以使用 <code>options</code> 参数进行自定义:</p> + +<pre class="brush: js">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 +</pre> + +<h2 id="性能">性能</h2> + +<p>格式化大量数字时,最好创建 {{jsxref("NumberFormat")}} 对象并使用其 {{jsxref("NumberFormat.format")}} 属性提供的函数。</p> + +<h2 id="标准">标准</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td><a href="https://tc39.es/ecma402/#sup-bigint.prototype.tolocalestring">BigInt</a></td> + <td>Stage 3</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.BigInt.toLocaleString")}}</p> + +<h2 id="请参阅">请参阅</h2> + +<ul> + <li>{{jsxref("BigInt.toString()")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>toString()</code></strong> 方法返回一个字符串,表示指定 {{jsxref("BigInt")}} 对象。 后面的 "n" 不是字符串的一部分。</p> + +<div>{{EmbedInteractiveExample("pages/js/bigint-tostring.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>bigIntObj</var>.toString([<var>radix</var>])</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>radix</code>{{Optional_inline}}</dt> + <dd>可选,介于 2 到 36 之间的整数,指定用于表示数值的基数。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>表示指定 {{jsxref("BigInt")}} 对象的字符串。</p> + +<h3 id="异常">异常</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd>如果 <code>toString()</code> 的基数小于 2 或大于 36, 则抛出 {{jsxref("RangeError")}}。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>{{jsxref("BigInt")}} 对象重写 {{jsxref("Object")}} 对象的 <code>toString()</code> 方法;它不继承 {{jsxref("Object.prototype.toString()")}}。对于 {{jsxref( "BigInt")}} 对象,<code>toString()</code> 方法返回指定基数中对象的字符串表示形式。</p> + +<p><code>toString()</code> 方法解析其第一个参数,并尝试返回指定基数(base)的字符串表示形式。对于大于 10 的参数,使用字母表中的字母表示大于 9 的数字。例如,对于十六进制数(以16为基数),使用 a 到 f。</p> + +<p>如果未指定基数,则假定首选基数为10。</p> + +<p>如果 <code>bigIntObj</code> 为负,则保留符号。即使基数是 2,情况也是如此;返回的字符串是 <code>bigIntObj</code> 的正二进制表示,前面是一个 <code>-</code> 符号,而不是 <code>bigIntObj</code> 的两个补码。</p> + +<h2 id="例子">例子</h2> + +<h3 id="Using_toString">Using <code>toString</code></h3> + +<pre class="brush: js">17n.toString(); // '17' +66n.toString(2); // '1000010' +254n.toString(16); // 'fe' +-10n.toString(2); // -1010' +-0xffn.toString(2); // '-11111111' +</pre> + +<h3 id="Negative-zero_BigInt">Negative-zero <code>BigInt</code></h3> + +<p>没有负零 <code>BigInt</code>,因为整数中没有负零。<code>-0.0</code> 是一个 IEEE 浮点概念,只出现在JavaScript {{jsxref("Number")}} 类型中。</p> + +<pre class="brush: js">(-0n).toString(); // '0' +BigInt(-0).toString(); // '0'</pre> + +<h2 id="标准">标准</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint.prototype.tostring', 'BigInt.prototype.toString()')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.BigInt.toString")}}</p> + +<h2 id="请参阅">请参阅</h2> + +<ul> + <li>{{jsxref("BigInt.prototype.toLocaleString()")}}</li> + <li>{{jsxref("BigInt.prototype.valueOf()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>valueOf()</code></strong> 方法返回 {{jsxref("BigInt")}} 对象包装的原始值。</p> + +<div>{{EmbedInteractiveExample("pages/js/bigint-valueof.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>bigIntObj</var>.valueOf()</pre> + +<h3 id="返回值">返回值</h3> + +<p>表示指定 {{jsxref("BigInt")}} 对象的原始 BigInt 值。</p> + +<h2 id="例子">例子</h2> + +<h3 id="Using_valueOf">Using <code>valueOf</code></h3> + +<pre class="brush: js">typeof Object(1n); // object +typeof Object(1n).valueOf(); // bigint +</pre> + +<h2 id="标准">标准</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint.prototype.valueof', 'BigInt.prototype.valueOf()')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.BigInt.valueOf")}}</p> + +<h2 id="请参阅">请参阅</h2> + +<ul> + <li>{{jsxref("BigInt.prototype.toString()")}}</li> +</ul> |