generator函数

2019-12-05  本文已影响0人  凡凡的小web

function* test(){

console.log("start");

yield;

console.log("finish");

}

var fun1 = test(); //须将函数声明为变量使用此方法

fun1.next();

fun1.next();

yield*

yield和yield类似,只不过他是可以自动解构,用法如下
function
go(){
yield* [1, 2, 3];
yield* 'Hi';
yield* Array.from(arguments);
}

var g = go(4, 5);

console.log(g.next()); // {value: 1, done: false}
console.log(g.next()); // {value: 2, done: false}
console.log(g.next()); // {value: 3, done: false}
console.log(g.next()); // {value: "H", done: false}
console.log(g.next()); // {value: "i", done: false}
console.log(g.next()); // {value: 4, done: false}
console.log(g.next()); // {value: 5, done: false}
console.log(g.next()); // {value: undefined, done: true}

https://blog.csdn.net/qq_35530330/article/details/88831302

上一篇下一篇

猜你喜欢

热点阅读