javascript - Object.assign 不是函数

标签 javascript gulp ecmascript-6 babeljs

我将 babel 与 gulp 结合使用,并在 ES6 中创建了一个简单的 DOM 库。但是在运行之后,当我要使用它时,我在 chrome 控制台中得到了 Object.assign is not a function

这是gulp代码

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

这是类文件

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

        this.length = elements.length;

        Object.assign(this, elements);
    }

    ...

}

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

我在客户端使用它,例如 dom('#elId');

最佳答案

我想您已经知道,Google Chrome 使用 V8 ,它支持 ECMAScript 第 5 版。 Object.assign 在 ECMAScript 第 6 版中引入。

为了使用这些添加项,您需要 include the ES6 polyfill由 Babel 提供:

This will emulate a full ES6 environment. [...]

Available from the browser-polyfill.js file within a babel-core npm release. This needs to be included before all your compiled Babel code. You can either prepend it to your compiled code or include it in a <script> before it.

关于javascript - Object.assign 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764414/

相关文章:

javascript - 我无法让基本的 ES6 导入工作

javascript - 一种使用 .load 和通配符的方法

javascript - Handlebars : multiple conditions IF statement?

css - Gulp.js - Gulp 不更新浏览器上的 CSS 文件

twitter-bootstrap - LESS & Bootstrap : error evaluating function `darken` : color. toHSL 不是文件中的函数

javascript - Babel 中的 d3 事件回调上下文发生变化

javascript - array.concat 与 angular.copy

javascript - 我应该在我的网站中包含 "prefix-free"插件吗

node.js - gulpfile Visual Studio 2015 中的 npm del 错误

javascript - Angular2 - 没有 ExceptionHandler 的提供者