栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SyntaxError:意外标识符(ES6中的生成器)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SyntaxError:意外标识符(ES6中的生成器)

总结注释:不能

yield
常规 函数中使用,因此不能
yield
与一起使用
forEach
。这里是“生成的” foreach的示例:

function * foreach (arr, fn) {  var i  for (i = 0; i < arr.length; i++) {    yield * fn(arr[i])  }}function * gen (number) {  yield number + 1  yield number + 2  yield number + 3}function * other () {  yield * foreach([1, 2, 3], gen)}for (var i of other()) {    console.log(i)}

更新 也可以使用这样的助手很好地解决原始问题:

var nodes = {  type: 'root',  value: [    { type: 'char', value: 'a' },    { type: 'char', value: 'b' },    { type: 'root', value: [        { type: 'char', value: 'c' },        { type: 'char', value: 'd' },        { type: 'char', value: 'e' },      ]     },  ],}function * foreach (arr, fn) {  var i  for (i = 0; i < arr.length; i++) {    yield * fn(arr[i])  }}function * value (val) {  yield val}function * recursiveGenerator(node) {  yield * node.type === 'root' ?  foreach(node.value, recursiveGenerator) : value(node.value)}for (var generated of recursiveGenerator(nodes)) {  console.log(generated);}

因此,发电机本身变成了单线!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号