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

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

@@ -4,7 +4,7 @@
<p><code><strong>lastIndexOf()</strong></code> 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找<code>fromIndex</code> 处开始。</p>
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/array-lastindexof.html" width="100%"></iframe></div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><code><em>arr</em>.lastIndexOf(<em>searchElement</em>[, <em>fromIndex = arr.length - 1</em>])</code></pre>
<pre><code class="language-javascript"><code><em>arr</em>.lastIndexOf(<em>searchElement</em>[, <em>fromIndex = arr.length - 1</em>])</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>searchElement</code></dt>
@@ -21,7 +21,7 @@
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_lastIndexOf" name="Example:_Using_lastIndexOf">例子:使用 lastIndexOf</h3>
<p>下例使用 <code>lastIndexOf</code> 定位数组中的值。</p>
<pre class="brush: js">var array = [2, 5, 9, 2];
<pre><code class="language-javascript">var array = [2, 5, 9, 2];
var index = array.lastIndexOf(2);
// index is 3
index = array.lastIndexOf(7);
@@ -34,10 +34,10 @@ index = array.lastIndexOf(2, -2);
// index is 0
index = array.lastIndexOf(2, -1);
// index is 3
</pre>
</code></pre>
<h3 id="Example:_Finding_all_the_occurrences_of_an_element" name="Example:_Finding_all_the_occurrences_of_an_element">例子:查找所有元素</h3>
<p>下例使用 <code>lastIndexOf</code> 查找到一个元素在数组中所有的索引(下标),并使用 <a href="Reference/Global_Objects/Array/push" title="push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。"><code>push</code></a> 将所有添加到另一个数组中。</p>
<pre class="brush: js">var indices = [];
<pre><code class="language-javascript">var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array.lastIndexOf(element);
@@ -50,7 +50,7 @@ while (idx != -1) {
console.log(indices);
// [4, 2, 0];
</pre>
</code></pre>
<p>注意,我们要单独处理<code>idx==0</code>时的情况,因为如果是第一个元素,忽略了<code>fromIndex</code>参数则第一个元素总会被查找。这不同于<a href="Reference/Global_Objects/Array/indexOf" title="indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。"><code>indexOf</code></a>方法</p>
<p>注:原英文是针对使用三元操作符语句的作用进行说明的:<br/>
<code>idx = (idx &gt; 0 ? array.lastIndexOf(element, idx - 1) : -1);</code><br/>
@@ -58,7 +58,7 @@ console.log(indices);
 </p>
<h2 id="Compatibility" name="Compatibility">兼容旧环境Polyfill</h2>
<p><code>lastIndexOf</code> 在 ECMA-262 标准第 5 版被添加。因此它在不兼容该标准的浏览器中可能不被支持。你可以把下面代码添加到脚本中来使那些没有实现该方法的实现环境支持该方法。该算法是被 ECMA-262 第 5 版指定的。假定 <a href="Reference/Global_Objects/Object" title="Object 构造函数创建一个对象包装器。"><code>Object</code></a><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a><a href="Reference/Global_Objects/Math/floor" title="Math.floor() 返回小于或等于一个给定数字的最大整数。"><code>Math.floor</code></a><a href="Reference/Global_Objects/Math/abs" title="Math.abs(x) 函数返回指定数字 “x“ 的绝对值。如下:"><code>Math.abs</code></a>,以及 <a href="Reference/Global_Objects/Math/min" title="Math.min() 返回零个或更多个数值的最小值。"><code>Math.min</code></a> 拥有其初始值。</p>
<pre class="brush: js">if (!Array.prototype.lastIndexOf) {
<pre><code class="language-javascript">if (!Array.prototype.lastIndexOf) {
  Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
    'use strict';
@@ -95,7 +95,7 @@ console.log(indices);
  };
}
</pre>
</code></pre>
<p>另外该实现是为了绝对兼容 Firefox 和 the SpiderMonkey JavaScript 引擎中的 <code>lastIndexOf</code>,包括了几种临界情况。如果你要在实际应用中使用该实现,可以忽略这些临界情况,从而简化 <code>fromIndex</code> 的计算。</p>
<h2 id="规范">规范</h2>
<table class="standard-table">