Created
November 2, 2019 15:02
-
-
Save joe-oli/91e46bd84e2fc5a311dd3607e2fb37ed to your computer and use it in GitHub Desktop.
for module export types
This file contains hidden or 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
== 1. Name exports == | |
//------ lib.js ------ | |
export const sqrt = Math.sqrt; | |
export function square(x) { | |
return x * x; | |
} | |
export function diag(x, y) { | |
return sqrt(square(x) + square(y)); | |
} | |
//------ main.js ------ | |
import { square, diag } from 'lib'; | |
console.log(square(11)); // 121 | |
console.log(diag(4, 3)); // 5 | |
or | |
//------ main.js ------ | |
import * as lib from 'lib'; | |
console.log(lib.square(11)); // 121 | |
console.log(lib.diag(4, 3)); // 5 | |
== 2. Default exports (one per module) == | |
//------ myFunc.js ------ | |
export default function () { ... }; | |
//------ main1.js ------ | |
import myFunc from 'myFunc'; | |
myFunc(); | |
== 3. Mixed named & default exports == | |
//------ underscore.js ------ | |
export default function (obj) { | |
... | |
}; | |
export function each(obj, iterator, context) { | |
... | |
} | |
export { each as forEach }; | |
//------ main.js ------ | |
import _, { each } from 'underscore'; | |
... | |
== 4. Cyclical Dependencies == | |
// lib.js | |
import Main from 'main'; | |
var lib = {message: "This Is A Lib"}; | |
export { lib as Lib }; | |
// main.js | |
import { Lib } from 'lib'; | |
export default class Main { | |
// .... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment