Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

javascript - Why does Object.assign() require a polyfill when babel-loader is being used?

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

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...