ES6(ES2015) -Generators

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);

你可能感兴趣的:(web前端,generator,ES6)