myplayaaaaa
Version:
wwww
28 lines (26 loc) • 819 B
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//创建一个generator函数
function* test(){
//yield语句的返回值是undefined
let x = yield "tom"; // 暂停--阻塞
let y= yield "jarry"; //暂停
let z= yield "susan"; //暂停
return x+y+z;
}
//调用generator函数返回一个遍历器对象
let iter=test();
//调用iter遍历器对象的next方法,遍历generator函数内部每一个yield状态
console.log(iter.next());//{value:"tom",done:false}
console.log(iter.next(1));//{value:"jarry",done:false}
console.log(iter.next(2));//{value:"susan",done:false}
console.log(iter.next(3));//{value:6,done:true}
</script>
</head>
<body>
</body>
</html>