Object.assign不是一个函数 [英] Object.assign is not a function

查看:145
本文介绍了Object.assign不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用babel与gulp并在ES6中创建一个简单的DOM库。但运行后,当我要使用它时,我在chrome控制台中得到了 Object.assign不是一个函数



这是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 =>新的DOM(选择器);

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

解决方案

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



为了使用这些添加,您需要包括由Babel提供的ES6 polyfill


这将模拟一个完整的ES6环境。 [...]



可从中的 browser-polyfill.js babel-core npm发行。这需要在所有编译的Babel代码之前被包含。您可以将其添加到已编译的代码中,或将其包含在< script> 之前。



I'm using babel with gulp and create a simple DOM library in ES6. But after running and when i'm going to use it, I got the Object.assign is not a function in chrome console.

this is the gulp code

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');

解决方案

As I suspect you already know, Google Chrome uses V8, which supports ECMAScript 5th edition. Object.assign is introduced in ECMAScript 6th edition.

In order to use these additions, you need to include the ES6 polyfill provided by 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.

这篇关于Object.assign不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆