Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Last active August 29, 2015 14:23
Show Gist options
  • Save afonsomatos/8f8237cf907212783a18 to your computer and use it in GitHub Desktop.
Save afonsomatos/8f8237cf907212783a18 to your computer and use it in GitHub Desktop.
Importing and exporting modules in es6
// --- All ways to import a module (main.js)
import MyDefault from './m1';
import { square, diag } from './m1'
import { square as sq, diag as dg} from './m1'
import * as lib from './m1'
import { default as foo } from './m1'
import Animal from './m1';
import './m1' // loads the module (executes the body)
// but doesn't import anything
import MyDefault, * as lib from './m1'
import MyDefault, { square, diag } from './m1'
// --- All ways to export from a module (m1.js)
// |- Export default
// |-- Declarations
export default function () { ... } (no semicolon)
export default class { ... } (no semicolon)
// |-- Expressions (can be used internally in the module)
export default myFunction;
export default (function () { });
// |- Multiple exports
export let c = 3;
export const d = 4;
export var e = 5;
export { myVariable as myAlias, otherVariable as otherAlias}
export { defaultVariable as default, myFunc as bark }
// Re-exporting (adding another module's exports to those of
// the current module)
export * from './m2';
export { foo, bar as dog } from './m2';
// Export the default of the default of other module
export { default } from 'foo';
export { myFunc as default } from 'foo';
// Module imports are hoisted (moved to the beggining, internally)
// -- Values imported are bindings
// It's not possible to change a value within
// another module, use functions for that
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment