为什么在使用babel-loader时,Object.assign()需要一个polyfill? [英] Why does Object.assign() require a polyfill when babel-loader is being used?

查看:422
本文介绍了为什么在使用babel-loader时,Object.assign()需要一个polyfill?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在由Babel编译的ESP Web应用程序中使用webpack使用 Object.assign(),但是我收到错误:

I'm attempting to use Object.assign() in an ES6 web app compiled by Babel with webpack, but I'm getting an error:

Uncaught TypeError: Object.assign is not a function

我已经在使用 babel-loader 将ES6转移到ES5,所以我所有的其他ES6代码正在工作。然而,在我的代码库中, Object.assign()仅在我还导入babel-core / polyfill之后才起作用。我看到我也可以通过导入babel-运行时间,但我想了解为什么 Object.assign()需要多于 babel -loader 执行 - 不应该 babel-loader 预处理所有内容,包括 Object.assign()

I'm already using babel-loader to transpile ES6 to ES5, so all my other ES6 code is working. Yet, Object.assign() only works after I also import "babel-core/polyfill" in my codebase. I see that I can also fix this by importing babel-runtime, but I'd like to understand why Object.assign() requires more than what babel-loader performs — shouldn't babel-loader preprocess everything, including Object.assign()?

推荐答案

Babel通过 babel-loader 在ES6语法中。 Babel自己绝对没有添加ES6标准库功能(如 Object.assign )。加载聚合物填充单独的聚合物填充 core-js ,但是您可以加载所需的任何polyfill。

Babel, via babel-loader, transpiles differences in ES6 syntax. Babel on its own does absolutely nothing to add in ES6 standard library functionality (like Object.assign). Loading the polyfill loads a separate polyfill core-js for you, but you can load any polyfill you want.

即使某些语法转换依赖于特定的polyfill功能来加载,因为一些语法依赖于算法以及在库代码中实现的行为。每个列表的 http://babeljs.io/docs/learn-es2015/ 上的ES6功能假定加载了什么标准库功能。

Even some syntax conversions rely on specific polyfill functionality to be loads, since some syntax relies on algorithms and behaviors implemented in library code. The ES6 features on http://babeljs.io/docs/learn-es2015/ each list what standard library functionality are assumed to have been loaded.

这篇关于为什么在使用babel-loader时,Object.assign()需要一个polyfill?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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