forked from adamszaruga/es6
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdemo.js
46 lines (32 loc) · 1010 Bytes
/
demo.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
// ES6 has its own way of handling modules
// You're already used to NodeJS modules:
// file1.js
let myVar = 2;
module.exports = myVar;
// file2.js
let number = require('./file1.js');
console.log(number) // "2"
// ES6 proposed a new syntax for modules, using the keywords "import", "export", "from", and "as"
// file1.js
export let myVar = 2;
// file2.js
import { myVar } from './file1.js';
console.log(myVar);
// In ES6 you're allowed to export multiple values from one file
// file1.js
export let myVar = 2;
export const PI = 3.14159;
// file2.js
import { myVar, PI } from './file1.js'; // Almost looks like destructuring, doesn't it?
console.log( myVar, PI )
// You can rename variables that you import using the "as" keyword;
import { myVar, PI as pie } from './file1.js';
console.log(pie);
// If you only want to export one value from a file, you can use the "default" keyword:
// file1.js
export default function hi() {
console.log('hi')
}
// file2.js
import hi from './file1.js';
hi();