Last active
February 12, 2022 16:47
-
-
Save danywalls/de1d8fb54bfdd4444a195bc8402ee9ab to your computer and use it in GitHub Desktop.
ES6 Basic Examples
This file contains 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
let name = 'John'; | |
let test; | |
name = 'Jack'; | |
const person = { | |
name: 'Jhon', | |
age: 33 | |
}; | |
console.log(person.name); | |
const nums = [1, 2, 3, 4]; | |
const sayHello = () => { | |
console.log('hello'); | |
}; | |
//short functions | |
const sayHelloShort = name => console.log(` Hello ${name}`); | |
sayHelloShort('danye'); | |
sayHello(); | |
const fruits = ['apple', 'orange', 'platano']; | |
//foreach | |
fruits.forEach((fruit, index) => console.log(`${fruit} ${index}`)); | |
//map | |
const singleFruit = fruits.map(fruit => fruit.slice(0, -1).toUpperCase()); | |
console.log(singleFruit); | |
//filters | |
const friuitsWithO = fruits.filter(f => f.includes('o')); | |
console.log(friuitsWithO); | |
const arr = [1, 2, 3]; | |
//spread operator | |
const arr2 = [...arr]; | |
console.log(arr2); | |
const person2 = { | |
name: 'Brad', | |
age: 35 | |
}; | |
const newPerson = { | |
...person2, | |
email: '' | |
}; | |
const { age, email } = newPerson; | |
console.log(name, email); | |
console.log(newPerson); | |
//promises | |
const prom1 = () => { | |
const p = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
document.getElementById('message').classList.add('blockui'); | |
console.log('first method async'); | |
resolve({ data: 'hello completed 1' }); | |
}, 2000); | |
}); | |
return p; | |
}; | |
const prom2 = idval => { | |
const p = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('second method async'); | |
let id = idval; | |
if (id > 3) { | |
resolve({ data: 'hello completed 1' }); | |
document.getElementById('message').classList.add('green'); | |
} else { | |
document.getElementById('message').classList.add('danger'); | |
setTimeout(() => { | |
document.getElementById('message').classList.remove('blockui'); | |
}, 2000); | |
reject({ error: `id is ${id}` }); | |
} | |
}, 2000); | |
}); | |
return p; | |
}; | |
prom1().then(prom2(1)); | |
//classes | |
class Dev { | |
constructor(name) { | |
this.name = name; | |
} | |
programming() { | |
return `Hello I'm programing ${this.name}`; | |
} | |
} | |
class Yuki extends Dev { | |
constructor(name) { | |
super(name); | |
} | |
} | |
const boris = new Yuki('boris'); | |
const dany = new Dev('dany walls'); | |
console.log(boris.programming()); | |
console.log(dany.programming()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment