[英]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 ababel-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>
。
babel-core
: babel-core
: $ npm install babel-core --save-dev
polyfill
module into your js: polyfill
模块导入您的js: import 'babel-core/polyfill';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.