mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 15:54:22 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/array-includes.html" width="100%"></iframe></div>
|
||||
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre><var>arr</var>.includes(<var>valueToFind[</var>, <var>fromIndex]</var>)</pre>
|
||||
<pre><var>arr</var>.includes(<var>valueToFind[</var>, <var>fromIndex]</var>)</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt><code>valueToFind</code></dt>
|
||||
@@ -24,21 +24,21 @@
|
||||
<p><strong>Note:</strong> Technically speaking, <code>includes()</code> uses the <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Same-value-zero_equality">sameValueZero</a></code> algorithm to determine whether the given element is found.</p>
|
||||
</div>
|
||||
<h2 id="示例">示例</h2>
|
||||
<pre class="brush: js">[1, 2, 3].includes(2); // true
|
||||
<pre><code class="language-javascript">[1, 2, 3].includes(2); // true
|
||||
[1, 2, 3].includes(4); // false
|
||||
[1, 2, 3].includes(3, 3); // false
|
||||
[1, 2, 3].includes(3, -1); // true
|
||||
[1, 2, NaN].includes(NaN); // true
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="fromIndex_大于等于数组长度">fromIndex 大于等于数组长度</h3>
|
||||
<p>如果 <code>fromIndex</code> 大于等于数组的长度,则会返回 <code>false</code>,且该数组不会被搜索。</p>
|
||||
<pre class="brush: js">var arr = ['a', 'b', 'c'];
|
||||
<pre><code class="language-javascript">var arr = ['a', 'b', 'c'];
|
||||
|
||||
arr.includes('c', 3); // false
|
||||
arr.includes('c', 100); // false</pre>
|
||||
arr.includes('c', 100); // false</code></pre>
|
||||
<h3 id="计算出的索引小于_0">计算出的索引小于 0</h3>
|
||||
<p>如果 <code>fromIndex </code>为负值,计算出的索引将作为开始搜索<code>searchElement</code>的位置。如果计算出的索引小于 0,则整个数组都会被搜索。</p>
|
||||
<pre class="brush: js">// array length is 3
|
||||
<pre><code class="language-javascript">// array length is 3
|
||||
// fromIndex is -100
|
||||
// computed index is 3 + (-100) = -97
|
||||
|
||||
@@ -47,15 +47,15 @@ var arr = ['a', 'b', 'c'];
|
||||
arr.includes('a', -100); // true
|
||||
arr.includes('b', -100); // true
|
||||
arr.includes('c', -100); // true
|
||||
arr.includes('a', -2); // false</pre>
|
||||
arr.includes('a', -2); // false</code></pre>
|
||||
<h3 id="作为通用方法的_includes()">作为通用方法的 includes()</h3>
|
||||
<p><code>includes()</code> 方法有意设计为通用方法。它不要求<code>this</code>值是数组对象,所以它可以被用于其他类型的对象 (比如类数组对象)。下面的例子展示了 在函数的 <a href="Reference/Functions/arguments">arguments</a> 对象上调用的 <code>includes()</code> 方法。</p>
|
||||
<pre class="brush: js">(function() {
|
||||
<pre><code class="language-javascript">(function() {
|
||||
console.log([].includes.call(arguments, 'a')); // true
|
||||
console.log([].includes.call(arguments, 'd')); // false
|
||||
})('a','b','c');</pre>
|
||||
})('a','b','c');</code></pre>
|
||||
<h2 id="Polyfill">Polyfill</h2>
|
||||
<pre class="brush: js">// https://tc39.github.io/ecma262/#sec-array.prototype.includes
|
||||
<pre><code class="language-javascript">// https://tc39.github.io/ecma262/#sec-array.prototype.includes
|
||||
if (!Array.prototype.includes) {
|
||||
Object.defineProperty(Array.prototype, 'includes', {
|
||||
value: function(valueToFind, fromIndex) {
|
||||
@@ -106,7 +106,7 @@ if (!Array.prototype.includes) {
|
||||
}
|
||||
});
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>译者注:polyfill 没有实现 对于 <code>NaN</code> 的检测。</p>
|
||||
<p>如果你需要支持那些不支持<code><a href="https://developer.mozilla.orgReference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>的废弃JavaScript 引擎,你最好不要 polyfill <code>Array.prototype</code> 方法,因为你不能使它们不可枚举。</p>
|
||||
<h2 id="规范">规范</h2>
|
||||
|
||||
Reference in New Issue
Block a user