zoukankan      html  css  js  c++  java
  • 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());

    报错

    "D:Program FilesJetBrainsWebStorm 2017.2.5in
    unnerw.exe" "D:Program Files
    odejs
    ode.exe" D:chengxuvuepro2distappclasssrclesson1.js
    D:chengxuvuepro2distappclasssrclesson1.js:34
    	var generator = /*#__PURE__*/regeneratorRuntime.mark(function generator() {
    	                             ^
    
    ReferenceError: regeneratorRuntime is not defined
        at Object.<anonymous> (D:chengxuvuepro2distappclasssrclesson1.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 }

      

  • 相关阅读:
    二进制数组
    model模块
    Decorator [ˈdekəreɪtə(r)] 修饰器/装饰器 -- 装饰模式
    箭头函数
    async [ə'zɪŋk] 函数
    cocos2d-x 事件分发机制 ——触摸事件监听
    算法:高速排序
    [zlib]_[0基础]_[使用Zlib完整解压zip内容]
    设计模式之状态模式(State)摘录
    编写Web Serviceclient訪问www.webxml.com.cn提供的服务
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/8011816.html
Copyright © 2011-2022 走看看