<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>generator生成器</title>
</head>
<body>
<script>
/*
* generator生成器
* 可以返回多次的函数
*/
// 斐波那契数列
function fib (max) {
let a = 0,b = 1,arr = [0, 1];
while (arr.length < max) {
[a,b] = [b,a+b];
arr.push(b);
}
return arr;
}
console.log(fib(5));
console.log(fib(10));
function* fib1 (max) {
let a = 0,b = 1, n = 0;
while (n < max) {
yield a;
[a, b] = [b, a+b];
n++;
}
return;
}
let f = fib(10);
console.log(f);
for(let x of f){
console.log(x);
}
</script>
</body>
</html>
es6 generator
最新推荐文章于 2023-03-16 15:08:24 发布