我将 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 ababel-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/