<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function* foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
var a = foo(5);
a.next() // Object{value:6, done:false}
a.next() // Object{value:NaN, done:false}
a.next() // Object{value:NaN, done:true}
var b = foo(5);
console.log(b.next()) // { value:6, done:false }
console.log(b.next(12)) // { value:8, done:false }
console.log(b.next(13)) // { value:42, done:true } 5+24+13
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function* foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
var a = foo(5);
a.next()
a.next()
a.next()
var b = foo(5);
console.log(b.next())
console.log(b.next(12))
console.log(b.next(13))
function* fun(x) {
var aa = yield x * 2
var bb = yield aa*2
return
}
var fun1 = fun(1)
var cc = fun1.next()
console.log(cc, 'cc')
var dd = fun1.next(cc.value)
console.log(dd, 'dd')
var ee = fun1.next()
console.log(ee, 'ee')
</script>
</body>
</html>
参考:https://es6.ruanyifeng.com/#docs/generator