Last active
November 2, 2018 09:10
-
-
Save gil00pita/a4571751cadd386d6486cb14627d285e to your computer and use it in GitHub Desktop.
ES6 Refresher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ARROW FUNCTIONS | |
function sayHello() { | |
console.log('Hello'); | |
} | |
const sayHello = name => console.log(`Hello ${name}`); | |
const fruits = ['Apples', 'Oranges', 'Grapes']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CLASSES | |
class Person { | |
constructor(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
greet() { | |
return `Hello, my name is ${this.name} and I am ${this.age}`; | |
} | |
} | |
const person1 = new Person('John', 33); | |
const person2 = new Person('Sara', 28); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DESTRUCTURING | |
const profile = { | |
name: 'John Doe', | |
address: { | |
street: '40 Main st', | |
city: 'Boston' | |
}, | |
hobbies: ['movies', 'music'] | |
}; | |
const { name, address, hobbies } = profile; | |
const { street, city } = profile.address; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FILTER | |
const people = [ | |
{ id: 1, name: 'Karen' }, | |
{ id: 2, name: 'Bob' }, | |
{ id: 3, name: 'Sharon' } | |
]; | |
const people2 = people.filter(person => person.id !== 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FOREACH | |
fruits.forEach((fruit, index) => console.log(fruit)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CONST & LET | |
let name = 'John'; | |
let test; | |
name = 'Jack'; | |
const person = { | |
name: 'John', | |
age: 33 | |
}; | |
person.name = 'Jack'; | |
const nums = [1, 2, 3, 4]; | |
nums.push(5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MAP | |
const singleFruit = fruits.map(fruit => fruit.slice(0, -1).toUpperCase()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MODULES | |
// file 1 (file1.js) | |
export const name = 'Jeff'; | |
export const nums = [1, 2, 3]; | |
export default Person; | |
// // file 2 (file2.js) | |
import { name, nums } from './file1'; | |
import Person from './file1'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPREAD | |
const arr = [1, 2, 3]; | |
const arr2 = [...arr, 4]; | |
const arr3 = [...arr.filter(num => num !== 2)]; | |
const person1 = { | |
name: 'Brad', | |
age: 36 | |
}; | |
const person2 = { | |
...person1, | |
email: '[email protected]' | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SUBCLASSES; | |
class Customer extends Person { | |
constructor(name, age, balance) { | |
super(name, age); | |
this.balance = balance; | |
} | |
info() { | |
return `${this.name} owes $${this.balance}.00`; | |
} | |
} | |
const customer1 = new Customer('Kevin', 32, 300); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TEMPLATE LITERALS | |
//PREVIOUS//////////////////////////////////////// | |
//EX1 | |
var name = 'Your name is ' + first + ' ' + last + '.' | |
var url = 'http://localhost:3000/api/messages/' + id | |
//EX2 | |
var roadPoem = 'Then took the other, as just as fair,\n\t' | |
+ 'And having perhaps the better claim\n\t' | |
+ 'Because it was grassy and wanted wear,\n\t' | |
+ 'Though as for that the passing there\n\t' | |
+ 'Had worn them really about the same,\n\t' | |
var fourAgreements = 'You have the right to be you.\n\ | |
You can only be you when you do your best.' | |
//ES6//////////////////////////////////////// | |
//EX1 | |
var name = `Your name is ${first} ${last}.` | |
var url = `http://localhost:3000/api/messages/${id}` | |
//EX2 | |
var roadPoem = `Then took the other, as just as fair, | |
And having perhaps the better claim | |
Because it was grassy and wanted wear, | |
Though as for that the passing there | |
Had worn them really about the same,` | |
var fourAgreements = `You have the right to be you. | |
You can only be you when you do your best.` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://demo.fabiobiondi.io/es6playground/ | |
https://webapplog.com/es6/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment