mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-19 17:45:42 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<p><strong><code>localeCompare()</code></strong> 方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。</p>
|
||||
<p><code>新的 locales 、</code> <code>options</code> 参数能让应用程序定制函数的行为即指定用来排序的语言。 <code>locales</code> 和 <code>options</code> 参数是依赖于具体实现的,在旧的实现中这两个参数是完全被忽略的。</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox"><code><var>referenceStr</var>.localeCompare(<var>compareString</var>[, <var>locales</var>[, <var>options</var>]])</code></pre>
|
||||
<pre><code class="language-javascript"><code><var>referenceStr</var>.localeCompare(<var>compareString</var>[, <var>locales</var>[, <var>options</var>]])</code></code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<p>查阅<a href="#浏览器支持">浏览器支持</a>部分来确定哪些浏览器支持 <code>locales</code> 参数和 <code>options</code> 参数, <a href="检查浏览器对扩展参数的支持">在功能检测中检查对 <code>locales</code> 、<code>options </code>参数的支持</a>。</p>
|
||||
<dl>
|
||||
@@ -60,7 +60,7 @@
|
||||
<p><strong>切勿依赖于 -1 或 1 这样特定的返回值。</strong>不同浏览器之间(以及不同浏览器版本之间)<strong> </strong>返回的正负数的值各有不同,因为W3C规范中只要求返回值是正值和负值,而没有规定具体的值。一些浏览器可能返回-2或2或其他一些负的、正的值。</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用_localeCompare()">使用 <code>localeCompare()</code></h3>
|
||||
<pre class="brush: js">// The letter "a" is before "c" yielding a negative value
|
||||
<pre><code class="language-javascript">// The letter "a" is before "c" yielding a negative value
|
||||
'a'.localeCompare('c');
|
||||
// -2 or -1 (or some other negative value)
|
||||
|
||||
@@ -71,10 +71,10 @@
|
||||
// "a" and "a" are equivalent yielding a neutral value of zero
|
||||
'a'.localeCompare('a');
|
||||
// 0
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="检查浏览器对扩展参数的支持">检查浏览器对扩展参数的支持</h3>
|
||||
<p><code>locales</code> 和 <code>options</code> 参数还没有被所有阅览器所支持。检查是否被支持, 使用 "i" 参数 (a requirement that illegal language tags are rejected) 判断是否有异常 <a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误,当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a>抛出:</p>
|
||||
<pre class="brush: js">function localeCompareSupportsLocales() {
|
||||
<pre><code class="language-javascript">function localeCompareSupportsLocales() {
|
||||
try {
|
||||
'foo'.localeCompare('bar', 'i');
|
||||
} catch (e) {
|
||||
@@ -82,20 +82,20 @@
|
||||
}
|
||||
return false;
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="使用_locales_参数">使用 <code>locales 参数</code></h3>
|
||||
<p>在不同的语言下 <code>localeCompare()</code> 所提供的结果是不一致的。 为了能让用户得到正确的比较值, 通过使用 <code>locales</code> 参数来提供要比较的语言 (and possibly some fallback languages) :</p>
|
||||
<pre class="brush: js">console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
|
||||
<pre><code class="language-javascript">console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
|
||||
console.log('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="使用_options_参数">使用 <code>options 参数</code></h3>
|
||||
<p><code>localeCompare()</code> 所提供的结果可以通过 <code>options</code> 参数来制定:</p>
|
||||
<pre class="brush: js">// in German, ä has a as the base letter
|
||||
<pre><code class="language-javascript">// in German, ä has a as the base letter
|
||||
console.log('ä'.localeCompare('a', 'de', { sensitivity: 'base' })); // 0
|
||||
|
||||
// in Swedish, ä and a are separate base letters
|
||||
console.log('ä'.localeCompare('a', 'sv', { sensitivity: 'base' })); // a positive value
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="性能相关">性能相关</h2>
|
||||
<p>当比较大量字符串时, 比如比较大量数组时, 最好创建一个<a href="Reference/Global_Objects/Collator" title="Intl.Collator 是用于语言敏感字符串比较的 collators构造函数。"><code>Intl.Collator</code></a> 对象并使用<a class="new" href="Reference/Global_Objects/Collator/compare" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>compare</code></a> 属性所提供的函数。</p>
|
||||
<h2 id="规范">规范</h2>
|
||||
|
||||
Reference in New Issue
Block a user