简体   繁体   中英

Properties of babel-polyfill

In many pieces of code I saw such expression:

 require('babel-polyfill').default;

What does it mean property default, and where I can find all properties that could be applied to babel-polyfill, because I didn't see in Babel official documentation usage of this option.

This is an ES6 module convention, where someone is setting the "default" export of the module to a specific object. In ES6 syntax, it's equivalent to:

import Module from 'babel-polyfill'

which will take the default export from babel-polyfill and put it in your current file as Module .

And internally in the babel-polyfill library, they are doing

exports.default = { some: 'Object' }

This is different than named exports, where you want to expose specific named things from your library:

exports.someThing = 'value';
...
import { someThing } from 'that-module';

You can console.log the results of both require('babel-polyfill') and require('babel-polyfill').default to see more. However, babel polyfill mainly provides polyfills in the global namespace, and modifies native prototypes like Array, and you don't use anything from it directly. Simply requiring it has side effects that add correct polyfills to the running Javascript environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM