ES2015 cheatsheet by @richistron
let foo = true;
if (foo) {
let bar = true;
console.log(bar); // true
}
console.log(true); // true
console.log(bar); // undefined
const PUBLIC_KEY = 'abc123';
new ApiCall({
url: '/user',
public: PUBLIC_KEY
});
new ApiCall({
url: '/posts',
public: PUBLIC_KEY
});
let version = 'ES2015';
let tpl = `
<h1> Hellow World </h1>
<p> ${ version } rocks! </p>
`
let dog = { name: 'dog' };
let cat = { name: 'cat' };
let animalsMap = new Map();
animalsMap.set(dog, 100);
animalsMap.set(cat, 9999);
console.log(animalsMap.get(dog)); // 100
console.log(animalsMap.get(cat)); // 9999
let dog = { name: 'dog' };
let cat = { name: 'cat' };
let animalsMap = new WeakMap();
animalsMap.set(dog, 100);
animalsMap.set(cat, 9999);
console.log(animalsMap.get(dog)); // 100
console.log(animalsMap.get(cat)); // 9999
console.log(animalsMap.delete(cat)); // true
console.log(animalsMap.has(cat)); // false
let options = Object.assign({ foo: true, bar: true }, {foo: false});
console.log(options); // { foo: false, bar: true }
let animals = ['dog', 'cat', 'bird'];
let [dog, cat, bird] = animals;
console.log(dog, cat, bird); // dog cat bird
let animals = ['dog', 'cat', 'bird'];
let [dog, , bird] = animals;
console.log(dog, bird); // dog bird
let animals = ['dog', 'cat', 'bird'];
for (let animal of animals) {
console.log(animal); // dog cat bird
}
let animals = [
{ name: 'barkie', isDog: false },
{ name: 'snowBall', isDog: false },
{ name: 'rex', isDog: true }
]
let dog = animals.find (animal) => {
return animal.isDog;
}
console.log(dog); // { name: 'rex', isDog: true }
let animals = new Set();
animals.add('dog');
animals.add('cat');
animals.add('dog'); // it will throw an error
console.log(animals.length); // 2
let weakAnimals = new WeakSet();
weakAnimals.add({ name: 'dog' });
let cat = { name: 'cat' };
weakAnimals.add(cat);
console.log(weakAnimals.has(cat)); // true
console.log(weakAnimals.delete(cat)); // true
class Animal {
constructor(){
this.bark();
}
bark(){
console.log('woof');
}
}
let dog = new Animal(); // woof
class Human extends Monkey{
constructor(){
super();
}
}
new Human();
export default class ApiCall {
constructor(){
//
}
}
class ApiCall {
constructor(){
//
}
}
export { ApiCall }
import ApiCall from './helpers/api-call'
function *animalsList() {
yield 'dog';
yield 'cat';
yield 'bird';
}
for (let animal of animalsList()) {
console.log(animal);
}
let animals = [...animalsList()];
console.log(name);
let [first, second] = animalsList();
console.log(first, second);
let animal = {
name: 'Dog',
bark: true
};
animal[Symbol.iterator] = *function() {
let properties = Object.keys(this);
for (let p of properties) {
yield this[p];
}
};