简体   繁体   English

Object.assign不是函数

[英]Object.assign is not a function

I'm using babel with gulp and create a simple DOM library in ES6. 我在gulp中使用babel,并在ES6中创建了一个简单的DOM库。 But after running and when i'm going to use it, I got the Object.assign is not a function in chrome console. 但是在运行之后,当我要使用它时,我得到了Object.assign is not a function chrome控制台中Object.assign is not a function

this is the gulp code 这是gulp代码

gulp.task('scripts', function() {
    return gulp.src(src + 'js/*.js')
      .pipe(babel())
      .pipe(concat('main.js'))
      .pipe(gulp.dest(dest + 'js'));
});

this is the class file 这是课程文件

class DOM {
    constructor( selector ) {
        var elements = document.querySelectorAll(selector);

        this.length = elements.length;

        Object.assign(this, elements);
    }

    ...

}

const dom = selector => new DOM(selector);

and I'm using it in client side like dom('#elId'); 我在客户端使用它,例如dom('#elId');

As I suspect you already know, Google Chrome uses V8 , which supports ECMAScript 5th edition. 我怀疑您已经知道,Google Chrome使用V8 ,它支持ECMAScript 5th版本。 Object.assign is introduced in ECMAScript 6th edition. 在ECMAScript第6版中引入了Object.assign

In order to use these additions, you need to include the ES6 polyfill provided by Babel: 为了使用这些添加,您需要包括 Babel提供的ES6 polyfill

This will emulate a full ES6 environment. 这将模拟完整的ES6环境。 [...] [...]

Available from the browser-polyfill.js file within a babel-core npm release. 可从babel-core npm版本中的browser-polyfill.js文件中获得。 This needs to be included before all your compiled Babel code. 必须在所有已编译的Babel代码之前将其包括在内。 You can either prepend it to your compiled code or include it in a <script> before it. 您可以将其添加到已编译的代码之前,也可以将其包含在<script>

  1. Install babel-core : 安装babel-core

$ npm install babel-core --save-dev

  1. Import polyfill module into your js: polyfill模块导入您的js:

import 'babel-core/polyfill';

  1. Use babel to compile your code! 使用babel编译您的代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM