[ES6] 15. Generators -- 2
Using for..of statement:
function* greeting(){
console.log(`Generators are "lazy"`);
yield "How";
console.log(`I'm not called until the second next`);
yield "are";
console.log(`Call me before "you"?`);
yield "you";
console.log(`Called when "done"`);
} let greeter = greeting();
for(word of greeter){
console.log(word);
} //How
//are
//you
You can see, it ouptu "How are you", because it grabs the value off of the next.
The same as:
let greeter = greeting();
console.log(greeter.next().value);
console.log(greeter.next().value);
console.log(greeter.next().value);
console.log(greeter.next().value);
Assign the yield to the variable:
function* greet(){
let friendly = yield "How";
console.log(friendly);
yield "are";
yield "you";
} var greeter = greet();
console.log(greeter.next().value);
console.log(greeter.next().value);
console.log(greeter.next().value); //How
//undefined
//are
//you
When you assign yield to friendly, it doesn't console out "How" but undefined.
Why undefined?:
The way that this works is that the next step through the iteration, so if I say "The heck," will basically send this back through and assign it to this friendly. If I log this out now you'll see I get "How," and then "The heck." That means you can start building things through the iteration process.
Add param to the next():
function* greet(){
let friendly = yield "How";
console.log(friendly);
yield "are";
yield "you";
} var greeter = greet();
console.log(greeter.next().value);
console.log(greeter.next("the hack").value);
console.log(greeter.next().value); //How
//the hack
//are
//you
function* greet(){
let friendly = yield "How";
friendly = yield friendly + "are";
yield friendly + "you?";
} var greeter = greet();
console.log(greeter.next().value);
console.log(greeter.next(" the heck ").value);
console.log(greeter.next(" silly ol'").value); //How
// the heck are
// silly ol you
Cannot pass the param to the first next():
function* greet(){
let friendly = yield "How";
friendly = yield friendly + "are";
yield friendly + "you?";
} var greeter = greet();
console.log(greeter.next("first").value);
It will show error message:
TypeError: Sent value to newborn generator.
Because you haven't given this a chance to run and iterate and go to the next step where you could actually pass in a value.
总之:yield返回的值是下一个next中所传进来的值。
Generators also help you work with infinite sequences:
function* graph(){
let x = 0;
let y = 0;
while(true){
yield {x:x, y:y}
x += 2;
y += 1;
}
} var graphGenerator = graph();
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
console.log(graphGenerator.next().value);
I can safely yield this X and Y point knowing confidently that this stuff isn't going to evaluate until the next step through after the yield process.
[ES6] 15. Generators -- 2的更多相关文章
- es6(15)--generator
//generator处理异步,下一步用next,遇到return或者yied就会停止 { //generator基本定义 let tell=function* (){ yield 'a'; yiel ...
- ES6新特性概览
本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...
- es6学习笔记入门总结
1.let const block 作用域 let 代替var 来声明块级作用域,没有变量提升,只在块内有作用 const 可以声明一个常量,类似于指针,指向某一个引用,这个常量并非一成不变的,但是不 ...
- ES6 主要的新特性
本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...
- ES6之路
从工作到现在,虽然是PHP出身,一直都和JS形影不离,从JQ和原生处理页面,到后来被angular1的MVVM模式惊艳到,再到弃angular转战vue,到现在使用react,一路走来,跳坑无数,现在 ...
- ES6 一些常用使用
//1.解构数组 let arr1 = ['apple', 'coffee', 'cake']; let [fruit, drink, dessert] = arr1; console.log(fru ...
- Express/Koa/Hapi
Express/Koa/Hapi 本文翻译自: https://www.airpair.com/node.js/posts/nodejs-framework-comparison-express-ko ...
- 【转载】Express、Koa、Hapi框架对比
中文翻译:http://ourjs.com/detail/5490db1c8a34fa320400000e 英文原文:https://www.airpair.com/node.js/posts/nod ...
- Node.js服务端框架谁才是你的真爱
1. Express 背景: Express, 疯一般快速(而简洁)的服务端JavaScript Web开发框架,基于Node.js和V8 JavaScript引擎. Express 是一个基于 No ...
随机推荐
- 求相同号码一天内的上网流量——mapreduce
上网数据 1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 246 ...
- 闲谈Future模式-订蛋糕
一. Future模式简介 Future有道翻译:n. 未来:前途:期货:将来时.我觉得用期货来解释比较合适.举个实际生活中例子来说吧,今天我女朋友过生日,我去蛋糕店准备给女朋友定个大蛋糕,超级大的那 ...
- Chapter6:函数
执行函数的第一步是(隐式地)定义并初始化它的形参.所以,函数最外层作用域中的局部变量也不能使用与函数形参一样的名字. 局部静态变量:在程序的执行路径第一次经过对象定义语句时初始化,并且直到程序终止才被 ...
- 数往知来 ASP.NET 模拟服务器:服务端_静态页面_动态页面的响应<十七>
一.客户端是怎么看到我们的网页的呢/ 在浏览器端,如果用汉语请求的是一普通的HTML网页,呢么我们的IIS服务器, 接收到请求以后,那么从IIS服务器所在的电脑区查找该HTML网页, 找到以后将该 ...
- Java基础 —— JavaScript
Javascript:基于对象与事件驱动的脚本语言,主要用于客户端 特点: 交互性:信息动态交互. 安全性:不能访问本地硬盘. 跨平台性:只要有浏览器就支持Javascript,与平台无关. Java ...
- 从数列1,2,3.......n 中 随意取几个数,使其和等于 m
//从数列1,2,3.......n 中 随意取几个数,使其和等于 m public static void Print(int n, int m, List<int> ...
- 使用android SpannableStringBuilder实现图文混排
项目开发中需要实现这种效果 多余两行,两行最后是省略号,省略号后面是下拉更多 之前用过的是Html.fromHtml去处理图文混排的,仅仅是文字后图片或者文字颜色字体什么的, 但是这里需要在最后文字的 ...
- java线性表学习笔记(一)
线性表是一种按顺序储存数据是的常用结构,大多数的线性表都支持以下的典型操作: 从线性表提取插入删除一个数据: 找出线性表中的某一个元素: 找出线性表中的元素: 确定线性表中是否包含某一个元素,确定线性 ...
- usb库文件usb_desc.c分析
参考<圈圈教你玩USB> usb协议中使用的是小端结构,所以实际数据在传输时是低字节在先的. 设备描述符的实现: 已知每个设备都必须有且仅有一个设备描述符,它的结构在USB协议中有详细的定 ...
- MSSQL手札四 MSSQL的函数
和oracle一样,sql也可以自己定义函数 一个返回值,引用DEMO如下: 编写一个函数,该函数,可以通过输入借书时间来判断是否到期,当借阅时间大于30天,返回已经过期:否则返回还未到期. CREA ...