[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 ...
随机推荐
- 1005acm罚时
ACM国际大学生程序设计竞赛是由国际计算机学会主办的,一项旨在展示大学生创新能力.团队精神和在压力下编写程序.分析和解决问题能力的年度竞赛.参赛队伍最多由三名参赛队员组成,竞赛中一般命题10-13题, ...
- Scrum流程
敏捷Scrum流程图: Sprint Planing Meeting: 1.Next Spring Goal; 2.Sprint Backlog; 3.Updated Product Backlog; ...
- Mysql字符串截取函数SUBSTRING的用法说明
感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ...
- 验证dictionary重复键
if (dict.ContainsKey("sadsa")) { }
- 前端复习-02-ajax原生以及jq和跨域方面的应用。
ajax这块我以前一直都是用现成的jq封装好的东西,而且并没有深入浅出的研究过,也没有使用过原生形式的实现.包括了解jsonp和跨域的相关概念但是依然没有实现过,其中有一个重要的原因我认为是我当时并不 ...
- 第二百零四天 how can i 坚持
我应该不会看错吧.最近媒体热炒小米衰落了,有必要那么大张旗鼓的报道吗?小米.华为,坚决看好小米.感觉华为品牌有些杂乱,在走三星的老路,小米有些苹果的影子,但是,多了个互联网.互联网... 未来孰优孰劣 ...
- SO_REUSEADDR和SO_REUSEPORT异同
文章内容来源于stackoverflow上的回答,写的很详细http://stackoverflow.com/questions/14388706/socket-options-so-reuseadd ...
- Linux下的grep搜索命令详解(一)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- AutoCAD DxfCode组码值说明
值 说明 -5 APP:永久反应器链 -4 APP:条件运算符(仅与 ssget 一起使用) -3 APP:扩展数据 (XDATA) 标记(固定) -2 APP:图元名参照(固定) -1 APP:图元 ...
- oracle学习 九 游标的使用(持续更)
为什么要使用? 笔者查阅了一些资料之后得到的结论是, 关系型数据库是面向集合的,而游标是面向行的,游标可对取出来的集合(结果集)中每一行进行相同或不同的操作,还提供对基于游标位置而对表中数据进行删除或 ...