Skip to content

Instantly share code, notes, and snippets.

@mitevpi
Created December 12, 2019 17:36
Show Gist options
  • Save mitevpi/ef18d1e61e1d6be5eece0644ae3d97fe to your computer and use it in GitHub Desktop.
Save mitevpi/ef18d1e61e1d6be5eece0644ae3d97fe to your computer and use it in GitHub Desktop.
Import/Export Functions
// 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
// NAMED EXPORT
//------ myFunc.js ------
export default function () { ... };
//------ main1.js ------
import myFunc from 'myFunc';
myFunc();
// MIXED
//------ underscore.js ------
export default function (obj) {
...
};
export function each(obj, iterator, context) {
...
}
export { each as forEach };
//------ main.js ------
import _, { each } from 'underscore';
// CYCLICAL
// 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