有事没事领个红包

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 }

  

 

posted @   crazyCodeLove  阅读(418)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示