简体   繁体   English

babel-polyfill的性质

[英]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. 这是什么意思,默认属性是什么,在哪里可以找到所有可以应用于babel-polyfill的属性,因为在Babel官方文档中没有看到此选项的用法。

This is an ES6 module convention, where someone is setting the "default" export of the module to a specific object. 这是ES6模块的约定,其中有人将模块的“默认”导出设置为特定的对象。 In ES6 syntax, it's equivalent to: 在ES6语法中,它等效于:

import Module from 'babel-polyfill'

which will take the default export from babel-polyfill and put it in your current file as Module . 这将从babel-polyfill中获取默认导出,并将其作为Module放入您当前的文件中。

And internally in the babel-polyfill library, they are doing 在babel-polyfill库内部,他们正在

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. 您可以console.log记录require('babel-polyfill')require('babel-polyfill').default以了解更多信息。 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. 但是,babel polyfill主要在全局名称空间中提供polyfill,并修改Array之类的原生原型,您不会直接使用它。 Simply requiring it has side effects that add correct polyfills to the running Javascript environment. 仅仅要求它具有副作用,即可向正在运行的Javascript环境中添加正确的polyfill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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