标签:fine chef function 迭代器 ret wan var ons turn
Iterator :
返回的结果是:{value, done}
function chef(foods){
let i = 0;
return {
next(){
let done = ( i> foods.length);
let value = !done foods[i++];undefined;
return{
value,
done
}
}
}
}
let wanghao = chef([‘西红柿‘,‘鸡蛋‘])
console.log(wanghao.next());
console.log(wanghao.next());
...
generator(生成器):
function* chef()
{
yeild ‘西红柿‘;
yeild ‘鸡蛋‘;
}
let wanghao = chef();
改造下:
function* chef(foods){
for(var i =0;i<foods.length;i++)
{
yeild foods[i]
}
}
var wanghao = chef([‘西红柿‘,‘鸡蛋‘])
console.log(wanghao.next());
console.log(wanghao.next());
...
再次学习 Iterator 迭代器 与 Generator 生成器
标签:fine chef function 迭代器 ret wan var ons turn
原文地址:https://www.cnblogs.com/eret9616/p/9222304.html