<h4 style ="text-align: center">for/each 循環(huán)</h4>
for/each 與 for/in 循環(huán)類似。但for/each并不遍歷對(duì)象的屬性,而是遍歷對(duì)象的值:
let o = {one:1, two:2, three:3};
for (let p in o) console.log(p); // 輸出 one, two, three
for each (let v in o) console .log(v); // 輸出 1, 2,3
for/in 循環(huán)遍歷索引, for/each循環(huán)遍歷元素
a = ['one', 'two', 'three'];
for (let p in a) console.log(p); //輸出indexes 0,1,2
for each (let v in a) console.log(v); // 輸出元素one, two, three
<h4 style ="text-align: center">迭代器iterator</h4>
javascript 1.7為for/in循環(huán)添加更多的功能,與python循環(huán)類似,可以便利任何可迭代(iterable)對(duì)象。
迭代器是一個(gè)對(duì)象,并允許對(duì)它的值集合進(jìn)行遍歷。它的接口很簡(jiǎn)單,一般擁有以下三個(gè)方法就可以了。
hasNext() //集合中是否還有下一個(gè)元素
next() //迭代到下一個(gè)元素
reset()//重置,我見到的代碼一般是拋出異常,即一般不支持多次迭代
但必須包含next()方法。
//counter()函數(shù)利用閉包特性實(shí)現(xiàn)了計(jì)數(shù)器當(dāng)前狀態(tài)的保存
function counter(start){
let nextValue = Math.round(start);
return {next: function (){ return nextValue++;}};
}
let numberGen = counter (100.1);
let ng1 = numberGen.next(); //100
let ng2 = numberGen.next(); //101
遍歷完調(diào)用next(),拋出StopIteration。StopIteration是全局變量屬性,值是普通對(duì)象(無屬性),只是為了終結(jié)迭代,并不推薦顯示處理。
可迭代對(duì)象必須定義一個(gè)名叫iterator()方法來獲得一個(gè)迭代器對(duì)象。for/in循環(huán)會(huì)自動(dòng)調(diào)用iterator()并自己處理StopIteration。
//返回一個(gè)可迭代對(duì)象,范圍內(nèi)數(shù)字
function range (min, max){
return {
get min(){return min;},
get max() {return max;},
__iterator__:function(){
let val = Math.ceil(min);
return {
next: function(){
if (val>max)
throw StopIteration;
return val++;
}
}
}
}
}
//迭代
for (let i in range (1,10)) console.log(i);//output 1- 10
如果想從一個(gè)可迭代對(duì)象中顯示獲得一個(gè)迭代器對(duì)象,只需要調(diào)用<strong>Iterator()</strong>函數(shù),保持代碼整潔。同時(shí),如果給<strong>Iterator()</strong>函數(shù)傳入第二個(gè)參數(shù),這個(gè)參與也會(huì)參與iterator()方法的調(diào)用。
如果傳入的對(duì)象沒有iterator(),或者數(shù)組沒有定義iterator()方法,<strong>Iterator()</strong>函數(shù)會(huì)返回這個(gè)對(duì)象的一個(gè)可迭代的自定義迭代器。
Iteroator()函數(shù)和結(jié)構(gòu)復(fù)制一起使用:
//方便的呢對(duì)對(duì)象或數(shù)組的屬性和值進(jìn)行遍歷
for(let [k,v] in Iterator({a:1,b:2}))
console.log(k + "=" + v); //輸出 "a=1" 和 "b=2"
<h5>Iteroator()函數(shù)兩個(gè)特性</h5>
<ul>
<li>只對(duì)自有屬性進(jìn)行遍歷而忽略繼承的屬性(prototype)</li>
<li>Iteroator()函數(shù)傳入第二個(gè)參數(shù)true,返回迭代器只對(duì)屬性名遍歷</li>
</ul>
<h3 style ="text-align: center">Genertor</h3>
<div style ="text-align: center">Generators: a better way to build Iterators.</div>
借助 <strong style ="">yield</strong>關(guān)鍵字,可以更優(yōu)雅的實(shí)現(xiàn)iterator。
在函數(shù)內(nèi)使用<strong style ="">yield</strong>,用法相當(dāng)于return:區(qū)別在于<strong style ="">yield</strong>產(chǎn)生一個(gè)可保持函數(shù)內(nèi)部狀態(tài)的值,另外生成器可以通過return終止函數(shù)但不能返回值。
//Fibonacci
function fib(){
let x =1, y =1;
while (true){
yield y;
[x,y] = [y,x+y];
}
}
//調(diào)用fib
f = fib();
for (let i = 0; i < 10; i++) console.log (f.next());
fib()沒有返回,所以不會(huì)拋出StopIteration,是一個(gè)無窮循環(huán)。
無窮循環(huán)生成器f會(huì)始終保持執(zhí)行狀態(tài),不使用時(shí)需要調(diào)用
f.close();
釋放生成器。
<strong style ="">send()/throw()</strong>
生成器在創(chuàng)建時(shí)初始化,傳入生成器函數(shù)的值是生成器所接收的唯一輸入。但每一個(gè)生成器都有一個(gè)send()方法用來重啟生成器執(zhí)行,類似于next()。與next()不同的是send()可以帶一個(gè)參數(shù)。
除next()&send()外,yield表達(dá)式還可以將參數(shù)作為異常拋給throw():
//send()增量計(jì)算
//throw("reset")重置初始量(不推薦)
function counter (initial){
let nextValue = initial;
while (true){
try{
let increment = yield nextValue;
if(increment)
nextValue+=increment;
else
nextValue++;
}
catch{
if (e === "reset") nextValue = initial;
else throw e;
}
}
let c = counter (10);
console.log(c.next()); // 10
console.log(c.send(4)): //14
console.log(c.throw("reset")) //10
}