-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_es6.js
More file actions
192 lines (156 loc) · 4.73 KB
/
Copy pathtutorial_es6.js
File metadata and controls
192 lines (156 loc) · 4.73 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// ES6 Tutorial
// var is global or local
// let is block scoped
var x = 1;
{
let x = 2;
console.log(x); // 2
}
console.log(x); // 1
// an array declared with var can be overwritten
var oldArr = ['a', 'b', 'c'];
oldArr = ['d', 'e', 'f'];
console.log(oldArr); // d, e, f
// It's a common misconception that const is immutable.
// const can't be reassigned, but a const array can be mutated
const myArr = [1, 2, 3];
myArr = [4, 5, 6]; // throws an error
myArr[0] = 0;
console.log(myArr); // 0, 2, 3
// Higher Order Arrow Functions
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
// Filter realNumberArray to return only positive integers. Then use map to return a new array with these integers squared.
return realNumberArray.filter((num) => num >= 0 && Number.isInteger(num))
.map((num) => num * num);
};
// Declare a const called squareIntegers to store the return value of squareList, with realNumberArray passed as its argument.
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers); // 16, 1764, 36
// Rest Operator
function countArgs(...args) {
return args.length + ' arguments passed.';
}
console.log(countArgs(1, 2, 3)); // 3 arguments passed.
const sum = (function() {
"use strict";
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum(2, 2, 5)); // 9
// Spread Operator
// ES5
var arr = [6, 89, 3, 45];
var maximus = Math.max(arr); // NaN
// apply is necessary because Math.max(arr) will take arr as a single NaN argument.
var maximus = Math.max.apply(null, arr); // 89
// ES6
// The spread operator will separate each index of the array to pass as individual arguments.
const arr = [6, 89, 3, 45];
const maximus = Math.max(...arr);
const minimus = Math.min(...arr);
console.log(maximus); // 89
console.log(minimus); // 3
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
"use strict";
arr2 = [...arr1];
})();
console.log(arr2); // JAN,FEB,MAR,APR,MAY
// Destructuring Assignment
// ES5
var voxel = {x: 3.6, y: 7.4, z: 6.54 };
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
// ES6
const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54
// You can also assign the values to different variables.
const { x : a, y : b, z : c } = voxel;
// assign value of x to a, y to b, z to c.
console.log(a); // 3.6
const AVG_TEMPERATURES = {
today: 77.5,
tomorrow: 79
};
function getTempOfTmrw(avgTemperatures) {
"use strict";
const { tomorrow : tempOfTomorrow } = avgTemperatures;
return tempOfTomorrow;
}
console.log(getTempOfTmrw(AVG_TEMPERATURES)); // 79
// Template Literals
// Use backtick ` not single quote '
// ES5 string concatenation
var name = 'Bob';
var myString = 'Hello ' + name + '!';
console.log(myString); // Hello Bob!
// ES6 template literals
let myName = 'Al';
let newString = `Hi there, ${myName}!`;
console.log(newString); // Hi there, Al!
const namesArr = ['Jack', 'Burt', 'Mandy'];
function greetEverybody() {
for(let i = 0; i < namesArr.length; i++) {
console.log(`Welcome, ${namesArr[i]}!`);
}
}
greetEverybody();
// Welcome, Jack!
// Welcome, Burt!
// Welcome, Mandy!
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
"use strict";
// change code below this line
const resultDisplayArray = arr.map(item => `<li class="text-warning">${item}</li>`);
// change code above this line
return resultDisplayArray;
}
const resultDisplayArray = makeList(result.failure);
console.log(resultDisplayArray);
/*
[ `<li class="text-warning">no-var</li>`,
`<li class="text-warning">var-on-top</li>`,
`<li class="text-warning">linebreak</li>` ]
*/
// ES5
const getMousePosition = (x, y) => ({
x: x,
y: y
});
//ES6
const getMousePosition = (x, y) => ({ x, y });
const getMousePosition = (x, y) => ({ x, y });
const newObject = (name) => ({
name,
sayHi() {
return `Hi. My name is ${this.name}`;
};
});
// ES6 Class Syntax
class SpaceShuttle {
constructor(targetPlanet){
this.targetPlanet = targetPlanet;
}
}
const zeus = new SpaceShuttle('Jupiter');
class Character {
constructor(name, job, equipment) {
this.name = name;
this.job = job;
this.equipment = equipment;
}
}
const erdrick = new Character('Erdrick', 'Warrior', ['Sword', 'Shield']);
console.log(`${erdrick.name} is a ${erdrick.job}.`); // Erdrick is a Warrior.
const rats = new Character('Rats', 'Bard', ['Bow', 'Lute']);
const yuusha = new Character('Yuusha', 'Hero', ['Boomerang', 'Gem']);
console.log(`${yuusha.name} is a ${yuusha.job}.`); // Yuusha is a Hero.