Saturday, January 2, 2016

ES6 Modules

Exporting and Importing

"export" keyword is used to expose parts of the code to other modules.
You can place "export" keyword in front of any variable, function or class to export it from the module.

Once you have module with exports, you can access the functionality in another module by using the "import" keyword.

The above example import only "sum" from the example module.
If you want to import multiple identifiers from the example module, you can explicitly list them out.

You can also import the entire module as a single object. All of the exports are then available on that object as properties.


Exporting and Importing defaults

The default value for a module is a single variable, function or class as specified by the "default" keyword.

This module exports a function as the default. The "default" keyword indicates that this is a default export and the function does't require a name because the module itself represent the function.

You can import a default value using the following syntax.

For modules that export both a default and one or more non-defaults. For instance suppose you have this module.

You can then import both "name" and the default function using the following.

Resource
https://leanpub.com/understandinges6/read#leanpub-auto-modules

No comments:

Post a Comment