我正在使用babel与gulp并在ES6中创建一个简单的DOM库.但运行后,当我要使用它时,我得到了Object.assign不是chrome控制台中的一个功能.
这是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版中引入.
为了使用这些添加,您需要由Babel提供include the ES6 polyfill:
This will emulate a full ES6 environment. […]
Available from the
browser-polyfill.jsfile within ababel-corenpm 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.