Generator

Generator(生成器) 是es6引入的新的数据类型,一个generator看上去像一个函数,但是可以返回多次。

形式上,Generator函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield 表达式,定义不同的内部状态(yield 在英文的意思是“产出”)

function* helloWorldGenerator() {
  yield ‘hello‘;
  yield  ‘world‘;
  return ‘ending‘;
}

var hw = helloWorldGenerator();

上面代码定义了一个Generator函数helloWorldGenerator,  它内部有两个yield 表达式(hello  和 world ), 即该函数有三个状态: hello, world  和 return 语句(结束执行)。

然后,Generator 函数的调用方法与普通函数一样,也是在函数名后面加上一对圆括号

Generator

上一篇:LVM与磁盘配额


下一篇:Kubesphere整合的Jenkins默认密码查看