语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -3,7 +3,7 @@
<p><code><strong>toLocaleString()</strong></code> 方法返回这个数字在特定语言环境下的表示字符串。</p>
<p>新的 <code>locales</code><code>options</code> 参数让应用程序可以指定要进行格式转换的语言,并且定制函数的行为。在旧的实现中,会忽略 <code>locales</code><code>options</code> 参数,使用的语言环境和返回的字符串的形式完全取决于实现方式。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><code><em>numObj</em>.toLocaleString(</code><code>[locales [, options]])</code></pre>
<pre><code class="language-javascript"><code><em>numObj</em>.toLocaleString(</code><code>[locales [, options]])</code></code></pre>
<h3 id="参数">参数</h3>
<p>查阅<a href="#Browser_Compatibility">浏览器兼容性</a>部分,了解哪些浏览器支持 <code>locales</code><code>options</code> 参数,通过<a href="#Checking_for_support_for_locales_and_options_arguments">示例: 检查 <code>locales</code><code>options</code> 参数的支持</a>了解特征检测。</p>
<div class="note">
@@ -54,13 +54,13 @@
<h2 id="示例">示例</h2>
<h3 id="使用_toLocaleString">使用 <code>toLocaleString</code></h3>
<p>在没有指定区域的基本使用时,返回使用默认的语言环境和默认选项格式化的字符串。</p>
<pre class="brush: js">var number = 3500;
<pre><code class="language-javascript">var number = 3500;
console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale
</pre>
</code></pre>
<h3 id="检查_locales_和_options_参数的支持">检查 <code>locales</code><code>options</code> 参数的支持</h3>
<p><code>locales</code><code>options</code> 参数目前还不是所有浏览器都支持的。在 ES5.1 和更新的实现中检查支持情况,可以依靠使用非法参数时规定抛出的 <a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a> 异常:</p>
<pre class="brush: js">function toLocaleStringSupportsLocales() {
<pre><code class="language-javascript">function toLocaleStringSupportsLocales() {
var number = 0;
try {
number.toLocaleString('i');
@@ -69,17 +69,17 @@ console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English loc
}
return false;
}
</pre>
</code></pre>
<p>早于 ES5.1 的实现中,如果带参数调用 <code>toLocaleString</code> 并不会抛出范围异常。</p>
<p>在所有宿主环境下,包括那些支持比 ed 5.1 还早的 ECMA-262 的环境,都能有效检测的方法是直接检测 ECMA-402 中的其它特性,它指定 <code>Number.prototype.toLocaleString</code> 需要支持地区选项:</p>
<pre class="brush: js">function toLocaleStringSupportsOptions() {
<pre><code class="language-javascript">function toLocaleStringSupportsOptions() {
return !!(typeof Intl == 'object' &amp;&amp; Intl &amp;&amp; typeof Intl.NumberFormat == 'function');
}
</pre>
</code></pre>
<p>它测试全局的 <code>Intl</code> 对象,检测它不是 <code>null</code> 并且有 <code>NumberFormat</code> 的方法。</p>
<h3 id="使用_locales">使用 <code>locales</code></h3>
<p>这个示例展示了不同地区数字格式的差异。为了设置你的应用程序界面下使用的语言格式,请确保使用 <code>locales</code> 参数指定了使用的语言(可能还有一些备用语言):</p>
<pre class="brush: js">var number = 123456.789;
<pre><code class="language-javascript">var number = 123456.789;
// 德国使用逗号作为小数分隔符,分位周期为千位
console.log(number.toLocaleString('de-DE'));
@@ -100,10 +100,10 @@ console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// 当请求不支持的语言时,例如巴厘语,加入一个备用语言,比如印尼语
console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789
</pre>
</code></pre>
<h3 id="使用_options">使用 <code>options</code></h3>
<p>通过 <code>toLocaleString</code> 返回的结果可以通过 <code>options</code> 参数进行定制:</p>
<pre class="brush: js">var number = 123456.789;
<pre><code class="language-javascript">var number = 123456.789;
// 要求货币格式
console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
@@ -116,7 +116,7 @@ console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY'
// 限制三位有效数字
console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
// → 1,23,000
</pre>
</code></pre>
<h2 id="性能">性能</h2>
<p>当格式化大量数字时,最好建立一个 <a href="Reference/Global_Objects/NumberFormat" title="Intl.NumberFormat是对语言敏感的格式化数字类的构造器类"><code>NumberFormat</code></a> 对象并且使用它提供的 <a href="Reference/Global_Objects/NumberFormat/format" title="Intl.NumberFormat.prototype.format 属性返回一个根据NumberFormat对象的语言环境和格式化选项来格式化一个数字的getter函数。"><code>NumberFormat.format</code></a> 方法。</p>
<h2 id="规范">规范</h2>