您的位置:首页 > 其它

ES6(ES2015) -Generators

2017-04-01 15:58 309 查看

Generators

function* graph(){
let x = 0;
let y = 0;
while(true){
yield  {x:x,y:y};
x += 2;
y += 2;
}
}

var m = graph();
console.log(m.next().value);
console.log(m.next('dasdasda').value);
console.log(m.next().value);
//Usage:
/*
greater.next().value 这个取的是yield 右侧的值
第二个greater.next('abc').value 的时候,'abc'这个字符串传给了friend这个变量
所以说,代码执行到yield右侧的时候就停止了
*/
function* great(){
let friend = yield "now";
console.log('friend',friend);
}
var greater = great();
console.log(greater.next().value);
console.log(greater.next('dasdasdas').value);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  generator ES6