-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6.js
53 lines (46 loc) · 1.46 KB
/
es6.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
// ------ ES6 (June 2015) ------
// Arrow functions
// Default parameters can be passed to functions
const newFunction = (currentName = 'Gabriel Pinto') => console.log('My name is', currentName);
newFunction();
newFunction('Gabo');
// Template strings and multiline
const firstName = 'Gabriel';
const lastName = 'Pinto';
console.log(`My name is ${firstName} ${lastName}.
I love JS.`);
// Improved object creation
const newPerson = { firstName, lastName };
// Object destructuring
const person = { name: 'Gabriel', age: 22, country: 'Colombia' };
const { name, age, country } = person;
// Spread operator
const oldMilan = ['Maldini', 'Nesta', 'Gattuso', 'Pirlo'];
const newMilan = ['Rebic', 'Romagnoli', 'Donnarumma', 'Bennacer'];
const milanStars = ['Zlatan', ...oldMilan, ...newMilan];
// Promises
const myPromise = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result = Math.random();
if (result <= 0.7) {
reject('Is not a valid grade');
} else {
resolve('Is a valid grade');
}
}, 200);
});
};
myPromise().then(data => { console.log(data); }).catch(err => { console.log('Error:', err) });
// Classes
class calculator {
constructor() {
this.firstValue = 0;
this.secondValue = 0;
}
sum(first, second) {
this.firstValue = first;
this.secondValue = second;
return this.firstValue + this.secondValue;
}
}