<div class="show-content-free">
<h4>1 遍历数组通常用for循环</h4>
<p>ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map、filter、some、every、reduce、reduceRight等,只不过他们的返回结果不一样。但是使用foreach遍历数组的话,使用break不能中断循环,使用return也不能返回到外层函数。</p>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-built_in">Array</span>.prototype.method=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.length);
}
<span class="hljs-keyword">var</span> myArray=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]
myArray.name=<span class="hljs-string">"数组"</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> index <span class="hljs-keyword">in</span> myArray) {
<span class="hljs-built_in">console</span>.log(myArray[index]);
}
</code></pre>
<h4>2 for in遍历数组的毛病</h4>
<p>1.index索引为字符串型数字,不能直接进行几何运算<br>
2.遍历顺序有可能不是按照实际数组的内部顺序<br>
3.使用for in会遍历数组所有的可枚举属性,包括原型。例如上栗的原型方法method和name属性<br>
所以for in更适合遍历对象,不要使用for in遍历数组。</p>
<p>那么除了使用for循环,如何更简单的正确的遍历数组达到我们的期望呢(即不遍历method和name),ES6中的for of更胜一筹.</p>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-built_in">Array</span>.prototype.method=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.length);
}
<span class="hljs-keyword">var</span> myArray=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]
myArray.name=<span class="hljs-string">"数组"</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> value <span class="hljs-keyword">of</span> myArray) {
<span class="hljs-built_in">console</span>.log(value);
}
</code></pre>
<p><em>记住,for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值。</em></p>
<p>for of遍历的只是数组内的元素,而不包括数组的原型属性method和索引name</p>
<h3>3 遍历对象</h3>
<p>遍历对象 通常用for in来遍历对象的键名</p>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-built_in">Object</span>.prototype.method=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>);
}
<span class="hljs-keyword">var</span> myObject={
<span class="hljs-attr">a</span>:<span class="hljs-number">1</span>,
<span class="hljs-attr">b</span>:<span class="hljs-number">2</span>,
<span class="hljs-attr">c</span>:<span class="hljs-number">3</span>
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">in</span> myObject) {
<span class="hljs-built_in">console</span>.log(key);
}
</code></pre>
<p>for in 可以遍历到myObject的原型方法method,如果不想遍历原型方法和属性的话,可以在循环内部判断一下,hasOwnPropery方法可以判断某属性是否是该对象的实例属性</p>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">in</span> myObject) {
<span class="hljs-keyword">if</span>(myObject.hasOwnProperty(key)){
<span class="hljs-built_in">console</span>.log(key);
}
}
</code></pre>
<p>同样可以通过ES5的Object.keys(myObject)获取对象的实例属性组成的数组,不包括原型方法和属性</p>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-built_in">Object</span>.prototype.method=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>);
}
<span class="hljs-keyword">var</span> myObject={
<span class="hljs-attr">a</span>:<span class="hljs-number">1</span>,
<span class="hljs-attr">b</span>:<span class="hljs-number">2</span>,
<span class="hljs-attr">c</span>:<span class="hljs-number">3</span>
}
</code></pre>
<h3>总结</h3>
<ul>
<li>for..of适用遍历数/数组对象/字符串/map/set等拥有迭代器对象的集合.但是不能遍历对象,因为没有迭代器对象.与forEach()不同的是,它可以正确响应break、continue和return语句</li>
<li>for-of循环不支持普通对象,但如果你想迭代一个对象的属性,你可以用for-in循环(这也是它的本职工作)或内建的Object.keys()方法:</li>
</ul>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">of</span> <span class="hljs-built_in">Object</span>.keys(someObject)) {
<span class="hljs-built_in">console</span>.log(key + <span class="hljs-string">": "</span> + someObject[key]);
}
</code></pre>
<ul>
<li>遍历map对象时适合用解构,例如;</li>
</ul>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> [key, value] <span class="hljs-keyword">of</span> phoneBookMap) {
<span class="hljs-built_in">console</span>.log(key + <span class="hljs-string">"'s phone number is: "</span> + value);
}
</code></pre>
<ul>
<li>
<strong>当你为对象添加myObject.toString()方法后,就可以将对象转化为字符串,同样地,当你向任意对象添加myObject<a href="" target="_blank">Symbol.iterator</a>方法,就可以遍历这个对象了。</strong><br>
举个例子,假设你正在使用jQuery,尽管你非常钟情于里面的.each()方法,但你还是想让jQuery对象也支持for-of循环,你可以这样做:</li>
</ul>
<pre class="hljs javascript"><code class="javascript">jQuery.prototype[<span class="hljs-built_in">Symbol</span>.iterator] = <span class="hljs-built_in">Array</span>.prototype[<span class="hljs-built_in">Symbol</span>.iterator];
</code></pre>
<p>所有拥有<a href="" target="_blank">Symbol.iterator</a>的对象被称为可迭代的。在接下来的文章中你会发现,可迭代对象的概念几乎贯穿于整门语言之中,不仅是for-of循环,还有Map和Set构造函数、解构赋值,以及新的展开操作符。</p>
<ul>
<li>for...of的步骤<br>
or-of循环首先调用集合的<a href="" target="_blank">Symbol.iterator</a>方法,紧接着返回一个新的迭代器对象。迭代器对象可以是任意具有.next()方法的对象;for-of循环将重复调用这个方法,每次循环调用一次。举个例子,这段代码是我能想出来的最简单的迭代器:</li>
</ul>
<pre class="hljs javascript"><code class="javascript"><span class="hljs-keyword">var</span> zeroesForeverIterator = {
[<span class="hljs-built_in">Symbol</span>.iterator]: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
},
<span class="hljs-attr">next</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> {<span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">0</span>};
}
};
</code></pre>
</div>