Slide 13
Slide 13 text
Iterator protocol
function infinite() {
let index = 0;
return {
next() {
return { value: index++,
done: false };
},
};
}
gjs> let o = {};
gjs> [...o]
TypeError: o[Symbol.iterator] is not a function
gjs> o[Symbol.iterator] = ''[Symbol.iterator]
gjs> [...o]
[,o,b,j,e,c,t, ,O,b,j,e,c,t,]
Iterators are not only returned from
generators, but any object can be one.
You can make any object iterable.