Voici deux écritures de Fibonacci :
const Fibonacci = {
n1: 1,
n2: 1,
[Symbol.iterator]() {
return this;
},
next() {
let current = this.n2;
this.n2 = this.n1;
this.n1 = this.n1 + current;
return {
value: current,
done: false
};
},
return (v) {
return {
value: v,
done: true
};
}
};
let Fib = {
[Symbol.iterator]() {
let n1 = 1,
n2 = 1;
return {
[Symbol.iterator]() {
return this;
},
next() {
let current = n2;
n2 = n1;
n1 = n1 + current;
return {
value: current,
done: false
};
},
return () {
console.log(
`stop`
);
return {
value: v,
done: true
};
}
};
}
};
for (var v of Fib) {
console.log(v);
if (v > 50) break;
}