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

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

@@ -6,7 +6,7 @@
<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>
<p>对于从左至右遍历的相似方法请参阅 <a href="Reference/Global_Objects/Array/reduce" title="reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。"><code>Array.prototype.reduce()</code></a>.</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><code><em>arr</em>.reduceRight(<em>callback</em>[, <em>initialValue</em>])</code></pre>
<pre><code class="language-javascript"><code><em>arr</em>.reduceRight(<em>callback</em>[, <em>initialValue</em>])</code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>callback</code></dt>
@@ -34,17 +34,17 @@
<h2 id="描述">描述</h2>
<p><code>reduceRight</code> 为数组中每个元素调用一次 <code>callback</code> 回调函数,但是数组中被删除的索引或从未被赋值的索引会跳过。回调函数接受四个参数:初始值(或上次调用回调的返回值)、当前元素值、当前索引,以及调用 <code>reduce</code> 的数组。</p>
<p>可以像下面这样调用 <code>reduceRight</code> 的回调函数 <code>callback</code></p>
<pre class="brush: js">array.reduceRight(function(previousValue, currentValue, index, array) {
<pre><code class="language-javascript">array.reduceRight(function(previousValue, currentValue, index, array) {
// ...
});
</pre>
</code></pre>
<p>首次调用回调函数时,<code>previousValue</code><code>currentValue</code> 可以是两个值之一。如果调用 <code>reduceRight</code> 时提供了 <code>initialValue</code> 参数,则 <code>previousValue</code> 等于 <code>initialValue</code><code>currentValue</code> 等于数组中的最后一个值。如果没有提供 <code>initialValue</code> 参数,则 <code>previousValue</code> 等于数组最后一个值, <code>currentValue</code> 等于数组中倒数第二个值。</p>
<p>如果数组为空,且没有提供 <code>initialValue</code> 参数,将会抛出一个 <code>TypeError 错误。如果数组只有一个元素且没有提供</code> <code>initialValue </code>参数,或者提供了 <code>initialValue</code> 参数,但是数组为空将会直接返回数组中的那一个元素或 <code>initialValue</code> 参数,而不会调用 <code>callback</code></p>
<p>该函数的完整执行过程见下例:</p>
<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
<pre><code class="language-javascript">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
</pre>
</code></pre>
<p>回调将会被调用四次,每次调用的参数及返回值如下:</p>
<table style="width: 100%;">
<thead>
@@ -94,10 +94,10 @@
</table>
<p><code>reduceRight</code> 返回值是最后一次调用回调的返回值(<code>10</code></p>
<p>如果提供了一个 <code>initialValue</code> 参数,则结果如下:</p>
<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
<pre><code class="language-javascript">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
}, 10);
</pre>
</code></pre>
<table style="width: 100%;">
<thead>
<tr>
@@ -155,27 +155,27 @@
<p><code>reduceRight</code> 返回值为 20。</p>
<h2 id="示例">示例</h2>
<h3 id="例子:求一个数组中所有值的和">例子:求一个数组中所有值的和</h3>
<pre class="brush: js">var total = [0, 1, 2, 3].reduceRight(function(a, b) {
<pre><code class="language-javascript">var total = [0, 1, 2, 3].reduceRight(function(a, b) {
return a + b;
});
// total == 6
</pre>
</code></pre>
<h3 id="例子扁平化flatten一个元素为数组的数组">例子扁平化flatten一个元素为数组的数组</h3>
<pre class="brush: js">var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
<pre><code class="language-javascript">var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
</pre>
</code></pre>
<h3 id="例子reduce_与_reduceRight_之间的区别">例子reduce 与 reduceRight 之间的区别</h3>
<pre class="brush: js">var a = ['1', '2', '3', '4', '5'];
<pre><code class="language-javascript">var a = ['1', '2', '3', '4', '5'];
var left = a.reduce(function(prev, cur) { return prev + cur; });
var right = a.reduceRight(function(prev, cur) { return prev + cur; });
console.log(left); // "12345"
console.log(right); // "54321"</pre>
console.log(right); // "54321"</code></pre>
<h2 id="兼容性旧环境Polyfill">兼容性旧环境Polyfill</h2>
<p><code>reduceRight</code> 被添加到 ECMA-262 标准第 5 版,因此它在某些实现环境中可能不被支持。把下面的代码添加到脚本开头可以解决此问题,从而允许在那些没有原生支持 <code>reduceRight</code> 的实现环境中使用它。</p>
<pre class="brush: js">// Production steps of ECMA-262, Edition 5, 15.4.4.22
<pre><code class="language-javascript">// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if ('function' !== typeof Array.prototype.reduceRight) {
Array.prototype.reduceRight = function(callback /*, initialValue*/) {
@@ -206,7 +206,7 @@ if ('function' !== typeof Array.prototype.reduceRight) {
return value;
};
}
</pre>
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>