Tutorial by Examples

In addition to named imports, you can provide a default export. // circle.js export const PI = 3.14; export default function area(radius) { return PI * radius * radius; } You can use a simplified syntax to import the default export. import circleArea from './circle'; console.log(circle...
Sometimes you have a module that you only want to import so its top-level code gets run. This is useful for polyfills, other globals, or configuration that only runs once when your module is imported. Given a file named test.js: console.log('Initializing...') You can use it like this: import '...
In ECMAScript 6, when using the module syntax (import/export), each file becomes its own module with a private namespace. Top-level functions and variables do not pollute the global namespace. To expose functions, classes, and variables for other modules to import, you can use the export keyword. /...
Given that the module from the Defining a Module section exists in the file test.js, you can import from that module and use its exported members: import {doSomething, MyClass, PI} from './test' doSomething() const mine = new MyClass() mine.test() console.log(PI) The somethingPrivate()...
In addition to importing named members from a module or a module's default export, you can also import all members into a namespace binding. import * as test from './test' test.doSomething() All exported members are now available on the test variable. Non-exported members are not available, j...
Sometimes you may encounter members that have really long member names, such as thisIsWayTooLongOfAName(). In this case, you can import the member and give it a shorter name to use in your current module: import {thisIsWayTooLongOfAName as shortName} from 'module' shortName() You can import m...
const namedMember1 = ... const namedMember2 = ... const namedMember3 = ... export { namedMember1, namedMember2, namedMember3 }

Page 1 of 1