-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules.js
72 lines (57 loc) · 1.7 KB
/
modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** Modules **/
// Strict Mode
// With ES6 module system, the Strict mode is turned on by default.
// We can follow the MDN documentation for more detail info on Strict Mode:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
/** Export **/
// NOTE: In order to be able to playaround I will use node syntax
// and add as a comment the equivalent in ES6.
/** Default Export **/
module.exports = 'foo'
// export default 'foo'
module.exports = NaN
// export default NaN
module.exports = { foo: 'Mr.Foo' }
// export default { foo: 'Mr.Foo' }
module.exports = () => 'Mr.Foo'
// export default () => 'Mr.Foo'
/** Naming Export **/
// Export one per file.
module.exports.foo = 'Mr.Foo'
// export var foo = 'Mr.Foo'
// Export several per file.
var foo = 'Mr.Foo'
var bar = 'Mr.Bar'
module.exports = {
foo: foo,
bar: bar
}
// export { foo, bar }
// Define export name.
var foo = 'Mr.Foo'
module.exports = { newFoo: foo }
// export { foo as myFoo }
/** Default Export with Naming **/
var api = {
foo: 'Mr.Foo',
bar: 'Mr.Bar'
}
module.exports = api
// export default api
/** Import **/
// All the above only makes sense along with import statement.
// Opposite to the export statement, which allow us to export values,
// the import statement allow us to use load and use those values in other modules.
/** Default Import **/
// export default 'foo'
// import * from '_moduleName_'
/** Naming Import **/
// One value.
// export var foo = 'Mr.Foo'
// import { foo } from '_moduleName_'
// Several values.
// export { foo: 'Mr.Foo', bar: 'Mr.Bar' }
// import { foo, bar } from '_moduleName_'
// With specific export name.
// export { foo as myFoo }
// import { myFoo } from '_moduleName_'