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());
...
原文地址:https://www.cnblogs.com/eret9616/p/9222304.html
时间: 2024-11-08 22:55:00