Last active
July 2, 2016 01:13
-
-
Save thomasboyt/5985333 to your computer and use it in GitHub Desktop.
es6 module blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES6 includes a new block keyword, "module", for defining modules | |
// in-line. | |
module "myModule" { | |
export default function() { | |
// You can export classes, functions, and other blocks. | |
return "Hello :)"; | |
} | |
// You can mix a default and named exports. | |
var foo = "I'm foo!"; | |
export foo; | |
// You can even export declarations directly. | |
export var bar = "I'm bar!"; | |
} | |
// import x from "y" imports the *default* export of y as x | |
// (and yes, this is comically unintuitive if you're used to Python | |
// modules - just pretend it's import y as x) | |
import helloWorld from "myModule"; | |
console.log(helloWorld()); // "Hello :)" | |
// import {x} from "y" imports the *named export* x | |
import {foo} from "myModule"; | |
console.log(foo); // "I'm foo!" | |
// "as" can be used to alias imports | |
import {foo as bar} from "myModule"; | |
console.log(bar); // "I'm foo!" | |
// You can also alias the default export | |
import {default as foo} from "myModule"; | |
console.log(foo); // "Hello :)" | |
// You can import multiple objects at once | |
import {default as helloWorld, foo, bar as baz} from "myModule" | |
console.log(helloWorld()) // "Hello :)" | |
console.log(foo); // "I'm foo!" | |
console.log(baz); // "I'm bar!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment