Skip to content

Commit 29a3595

Browse files
committed
chapter 01: ES2015 (ES6) examples
1 parent f9ed7d8 commit 29a3595

17 files changed

+194
-148
lines changed

examples/chapter01/11-ES6letconst.html renamed to examples/chapter01/11-ES2015-ES6-letconst.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="11-ES6letconst.js"></script>
8+
<script type="text/javascript" src="11-ES2015-ES6-letconst.js"></script>
99

1010
</body>
11-
</html>
11+
</html>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// @ts-check
2+
3+
//* ****** EcmaScript 2015 (ES6): let and const keywords
4+
5+
//* ****** EcmaScript 2015 (ES6): let is the new var (https://goo.gl/he0udZ)
6+
var framework = 'Angular';
7+
var framework = 'React';
8+
console.log(framework);
9+
10+
let language = 'JavaScript!'; // {1}
11+
// let language = 'Ruby!'; // {2} - throws error
12+
console.log(language);
13+
14+
//* ****** EcmaScript 2015 (ES6): variables scope (https://goo.gl/NbsVvg)
15+
let movie = 'Lord of the Rings'; // {1}
16+
// var movie = 'Batman v Superman'; //throws error, variable movie already declared
17+
18+
function starWarsFan() {
19+
const movie = 'Star Wars'; // {2}
20+
return movie;
21+
}
22+
23+
function marvelFan() {
24+
movie = 'The Avengers'; // {3}
25+
return movie;
26+
}
27+
28+
function blizzardFan() {
29+
const isFan = true;
30+
let phrase = 'Warcraft'; // {4}
31+
console.log('Before if: ' + phrase);
32+
if (isFan) {
33+
let phrase = 'initial text'; // {5}
34+
phrase = 'For the Horde!'; // {6}
35+
console.log('Inside if: ' + phrase);
36+
}
37+
phrase = 'For the Alliance!'; // {7}
38+
console.log('After if: ' + phrase);
39+
}
40+
41+
console.log(movie); // {8}
42+
console.log(starWarsFan()); // {9}
43+
console.log(marvelFan()); // {10}
44+
console.log(movie); // {11}
45+
blizzardFan(); // {12}
46+
47+
// output
48+
// Lord of the Rings
49+
// Star Wars
50+
// The Avengers
51+
// The Avengers
52+
// Before if: Warcraft
53+
// Inside if: For the Horde!
54+
// After if: For the Alliance!
55+
56+
//* ****** EcmaScript 2015 (ES6): const (https://goo.gl/YUQj3r)
57+
const PI = 3.141593;
58+
// PI = 3.0; //throws error
59+
console.log(PI);
60+
61+
const jsFramework = {
62+
name: 'Angular'
63+
};
64+
jsFramework.name = 'React';
65+
66+
// error, cannot reassign object reference
67+
/*
68+
jsFramework = {
69+
name: 'Vue'
70+
};
71+
*/

examples/chapter01/11-ES6letconst.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/chapter01/15-ES6EnhancedObjectProperties.html renamed to examples/chapter01/12-ES2015-ES6-StringTemplates.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="15-ES6EnhancedObjectProperties.js"></script>
8+
<script type="text/javascript" src="12-ES2015-ES6-StringTemplates.js"></script>
99

1010
</body>
11-
</html>
11+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @ts-check
2+
3+
//* ****** EcmaScript 2015 (ES6): Template literals (https://goo.gl/4N36CS)
4+
const book = {
5+
name: 'Learning JavaScript DataStructures and Algorithms'
6+
};
7+
8+
console.log('You are reading ' + book.name + '.,\n and this is a new line\n and so is this.');
9+
10+
console.log(`You are reading ${book.name}.,
11+
and this is a new line
12+
and so is this.`);
13+

examples/chapter01/12-Es6StringTemplates.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/chapter01/13-ES6ArrowFunctions.html renamed to examples/chapter01/13-ES2015-ES6-ArrowFunctions.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="13-ES6ArrowFunctions.js"></script>
8+
<script type="text/javascript" src="13-ES2015-ES6-ArrowFunctions.js"></script>
99

1010
</body>
11-
</html>
11+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @ts-check
2+
3+
//* ****** EcmaScript 2015 (ES6): arrow functions (https://goo.gl/nM414v)
4+
var circleAreaES5 = function circleArea(r) {
5+
var PI = 3.14;
6+
var area = PI * r * r;
7+
return area;
8+
};
9+
console.log(circleAreaES5(2));
10+
11+
const circleArea = r => { // {1}
12+
const PI = 3.14;
13+
const area = PI * r * r;
14+
return area;
15+
};
16+
console.log(circleArea(2));
17+
18+
const circleArea2 = r => 3.14 * r * r;
19+
console.log(circleArea2(2));
20+
21+
const hello = () => console.log('hello!');
22+
hello();

examples/chapter01/13-ES6ArrowFunctions.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/chapter01/14-ES6ParameterHandling.html renamed to examples/chapter01/14-ES2015-ES6-ParameterHandling.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="14-ES6ParameterHandling.js"></script>
8+
<script type="text/javascript" src="14-ES2015-ES6-ParameterHandling.js"></script>
99
</body>
10-
</html>
10+
</html>

0 commit comments

Comments
 (0)