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

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 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><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/array-foreach.html" width="100%"></iframe></p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><var>arr</var>.forEach(<var>callback</var>[, <var>thisArg</var>]);</pre>
<pre><code class="language-javascript"><var>arr</var>.forEach(<var>callback</var>[, <var>thisArg</var>]);</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>callback</code></dt>
@@ -51,7 +51,7 @@
</div>
<h2 id="示例">示例</h2>
<h3 id="for_循环转换为_forEach">for 循环转换为 forEach</h3>
<pre class="brush:js">const items = ['item1', 'item2', 'item3'];
<pre><code class="language-js">const items = ['item1', 'item2', 'item3'];
const copy = [];
// before
@@ -63,13 +63,13 @@ for (let i=0; i&lt;items.length; i++) {
items.forEach(function(item){
copy.push(item);
});
</pre>
</code></pre>
<h3 id="打印出数组的内容">打印出数组的内容</h3>
<div class="blockIndicator note">
<p><strong>注意:</strong>为了在控制台中显示数组的内容,你可以使用 <code><a href="/zh-CN/docs/Web/API/Console/table">console.table()</a></code> 来展示经过格式化的数组。下面的例子则是另一种使用 <code>forEach()</code> 的格式化的方法。</p>
</div>
<p>下面的代码会为每一个数组元素输出一行记录:</p>
<pre class="brush:js">function logArrayElements(element, index, array) {
<pre><code class="language-js">function logArrayElements(element, index, array) {
console.log('a[' + index + '] = ' + element);
}
@@ -79,10 +79,10 @@ items.forEach(function(item){
// a[0] = 2
// a[1] = 5
// a[3] = 9
</pre>
</code></pre>
<h3 id="使用_thisArg">使用 <code>thisArg</code></h3>
<p>举个勉强的例子,从每个数组中的元素值中更新一个对象的属性:</p>
<pre class="brush:js">function Counter() {
<pre><code class="language-js">function Counter() {
this.sum = 0;
this.count = 0;
}
@@ -102,14 +102,14 @@ obj.count;
// 4 === (1+1+1+1)
obj.sum;
// 16 === (1+3+5+7)
</pre>
</code></pre>
<p>因为 <code>thisArg</code> 参数(<code>this</code>)传给了 <code>forEach()</code>,每次调用时,它都被传给 <code>callback</code> 函数,作为它的 <code>this</code> 值。</p>
<div>
<div class="note"><strong>注意:</strong>如果使用<a href="Reference/Functions/Arrow_functions">箭头函数表达式</a>来传入函数参数,<code>thisArg</code> 参数会被忽略,因为箭头函数在词法上绑定了 <a href="Reference/Operators/this" title="与其他语言相比,函数的 this 关键字在 JavaScript 中的表现略有不同,此外,在严格模式和非严格模式之间也会有一些差别。"><code>this</code></a> 值。</div>
</div>
<h3 id="对象复制函数">对象复制函数</h3>
<p>下面的代码会创建一个给定对象的副本。 创建对象的副本有不同的方法,以下是只是一种方法,并解释了 <code>Array.prototype.forEach()</code> 是如何使用 ECMAScript 5 <code>Object.*</code> 元属性meta property )函数工作的。</p>
<pre class="brush: js">function copy(obj) {
<pre><code class="language-javascript">function copy(obj) {
var copy = Object.create(Object.getPrototypeOf(obj));
var propNames = Object.getOwnPropertyNames(obj);
@@ -123,10 +123,10 @@ obj.sum;
var obj1 = { a: 1, b: 2 };
var obj2 = copy(obj1); // obj2 looks like obj1 now
</pre>
</code></pre>
<h3 id="如果数组在迭代时被修改了,则其他元素会被跳过。">如果数组在迭代时被修改了,则其他元素会被跳过。</h3>
<p>下面的例子会输出"one", "two", "four"。当到达包含值"two"的项时,整个数组的第一个项被移除了,这导致所有剩下的项上移一个位置。因为元素 "four"现在在数组更前的位置,"three"会被跳过。 <code>forEach()</code>不会在迭代之前创建数组的副本。</p>
<pre class="brush:js">var words = ['one', 'two', 'three', 'four'];
<pre><code class="language-js">var words = ['one', 'two', 'three', 'four'];
words.forEach(function(word) {
console.log(word);
if (word === 'two') {
@@ -136,10 +136,10 @@ words.forEach(function(word) {
// one
// two
// four
</pre>
</code></pre>
<h2 id="Polyfill">Polyfill</h2>
<p><code>forEach</code> 是在第五版本里被添加到 ECMA-262 标准的;这样它可能在标准的其他实现中不存在,你可以在你调用 <code>forEach</code> 之前 插入下面的代码,在本地不支持的情况下使用 <code>forEach()</code>。该算法是 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> 拥有它们的初始值。<code>callback.call</code> 等价于 <a href="Reference/Global_Objects/Function/call" title="call() 方法调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。"><code>Function.prototype.call()</code></a></p>
<pre class="brush: js">// Production steps of ECMA-262, Edition 5, 15.4.4.18
<pre><code class="language-javascript">// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
@@ -202,7 +202,7 @@ if (!Array.prototype.forEach) {
// 8. return undefined
};
}
</pre>
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>