es6 Generator函数的使用

function* foo(){
    yield 1;
    yield 2;
    yield 3;
}
var f = foo();
console.log(f.next());//{ value: 1, done: false }
console.log(f.return("foo"));//

function* callee() {
    console.log('callee: ' + (yield));
}
function* caller() {
    while (true) {
        yield* callee();
    }
}
const callerObj = caller();
console.log(callerObj.next());

function* objectEntries(obj) {
    const propKeys = Reflect.ownKeys(obj);
    for (const propKey of propKeys) {
        yield [propKey, obj[propKey]];
    }
}
 
const jane = { first: 'Jane', last: 'Doe' };
 
for (const [key,value] of objectEntries(jane)) {
    console.log(`${key}: ${value}`);
}

打印结果:
es6 Generator函数的使用_第1张图片

你可能感兴趣的:(es6)