es6编写generator报错
首先babel基础包(不安装额外东西)并不是支持完整的es6语言
自己写的如下代码
let generator = function* () { yield 1; yield * [2,3,4]; yield 5; }; var gen = generator(); console.log('1',gen.next()); console.log('2',gen.next()); console.log('3',gen.next());
报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | "D:\Program Files\JetBrains\WebStorm 2017.2.5\bin\runnerw.exe" "D:\Program Files\nodejs\node.exe" D:\chengxu\vue\pro2\dist\app\ class \src\lesson1.js D:\chengxu\vue\pro2\dist\app\ class \src\lesson1.js:34 var generator = /*#__PURE__*/ regeneratorRuntime.mark(function generator() { ^ ReferenceError: regeneratorRuntime is not defined at Object.<anonymous> (D:\chengxu\vue\pro2\dist\app\ class \src\lesson1.js:34:31) at Module._compile (module.js:635:30) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3 Process finished with exit code 1 |
自己的环境如下
{ "name": "pro2", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.24.1" } }
解决方法:在自己的项目js文件中最开头写上import "babel-polyfill"; 在运行,就可以成功运行。
1 { value: 1, done: false } 2 { value: 2, done: false } 3 { value: 3, done: false }
学习过程中,难免出错。如果您在阅读过程中遇到不太明白,或者有疑问。欢迎指正...联系邮箱crazyCodeLove@163.com
如果觉得有用,想赞助一下请移步赞助页面:赞助一下
分类:
前端学习小结
标签:
es6编写generator报错
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)