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

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

问题描述

我试图在由 Babel 和 webpack 编译的 ES6 Web 应用程序中使用 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() 仅在我也在我的代码库中 import "babel-core/polyfill" 后才有效.我看到我也可以通过导入 babel-来解决这个 运行时,但我想了解为什么 Object.assign() 需要的不仅仅是 babel-loader 执行的 - 应该't 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).加载 polyfill 会为你加载一个单独的 polyfill 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天全站免登陆