1function foo() { console.log('foo') }
2function bar() { console.log('bar') }
3function baz() { foo(); bar() }
4
5export default {foo, bar, baz}
1// helloworld.js
2
3export function helloWorld() {
4 return 'Hello World!';
5}
6
7// main.js
8
9import helloWorld from './helloworld.js';
10
11console.log(helloWorld());
1// Exporting individual features
2export let name1, name2, …, nameN; // also var, const
3export let name1 = …, name2 = …, …, nameN; // also var, const
4export function functionName(){...}
5export class ClassName {...}
6
7// Export list
8export { name1, name2, …, nameN };
9
10// Renaming exports
11export { variable1 as name1, variable2 as name2, …, nameN };
12
13// Exporting destructured assignments with renaming
14export const { name1, name2: bar } = o;
15
16// Default exports
17export default expression;
18export default function (…) { … } // also class, function*
19export default function name1(…) { … } // also class, function*
20export { name1 as default, … };
21
22// Aggregating modules
23export * from …; // does not set the default export
24export * as name1 from …; // Draft ECMAScript® 2O21
25export { name1, name2, …, nameN } from …;
26export { import1 as name1, import2 as name2, …, nameN } from …;
27export { default } from …;
1//import it
2import Example from './file2';
3//Create an Instance
4var myInstance = new Example()
5myInstance.test()
1export const name = 'square';
2
3export function draw(ctx, length, x, y, color) {
4 ctx.fillStyle = color;
5 ctx.fillRect(x, y, length, length);
6
7 return {
8 length: length,
9 x: x,
10 y: y,
11 color: color
12 };
13}
14
1// export a function in javascript
2export function myFunc(var1, var2, ..., varn) {
3 // do something
4}