Skip to content

Instantly share code, notes, and snippets.

@danywalls
Last active February 12, 2022 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danywalls/de1d8fb54bfdd4444a195bc8402ee9ab to your computer and use it in GitHub Desktop.
Save danywalls/de1d8fb54bfdd4444a195bc8402ee9ab to your computer and use it in GitHub Desktop.
ES6 Basic Examples
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