MUG040
new Date(2017, 10, 22);
Freelance Full Stack Web Developer
JavaScript, React, Node.js
Creator of Drammit and Formable
@GayaKessler on Twitter
function nameGenerator() {
var name = 'Gaya';
function giveName() {
return name;
}
return giveName;
}
var generate = nameGenerator(); // puts giveName() in generate
generate(); // Gaya
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000);
}
// 5
// 5
// 5
// 5
// 5
for (var i = 0; i < 5; i++) { // <-- create scope for 'i'
setTimeout(function timer() {
console.log(i);
}, i * 1000);
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000); // <-- 0 * 1000
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000); // <-- 1 * 1000
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000); // <-- 2 * 1000
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000); // <-- 3 * 1000
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i);
}, i * 1000); // <-- 4 * 1000
}
for (var i = 0; i < 5; i++) { // <-- i++ = 5
setTimeout(function timer() {
console.log(i);
}, i * 1000);
}
for (var i = 0; i < 5; i++) {
setTimeout(function timer() {
console.log(i); // 5
}, i * 1000);
}
for (var i = 0; i < 5; i++) {
(function (j) {
setTimeout(function timer() {
console.log(j);
}, j * 1000);
})(i);
}
function createTimer(j) {
return function timer() {
console.log(j);
}
}
for (var i = 0; i < 5; i++) {
setTimeout(createTimer(i), i * 1000);
}
this
?
function foo(cb) {
this.a = 'hey';
cb();
}
function bar() {
console.log(this.a);
}
var hi = bar;
hi.a = 'hi';
foo(hi); // hey
hi.a; // hi
function foo(cb) { // <-- `bar` is `cb`
this.a = 'hey';
cb(); // <-- call site
}
function bar() {
console.log(this.a); // <-- now `this` is `foo`
}
var hi = bar;
hi.a = 'hi';
foo(hi);
hi.a;
function foo(cb) {
this.a = 'hey';
cb(); // <-- still the same call site
}
function bar() {
console.log(this.a); // <-- but `this` is bound to `hi`
}
var hi = bar;
hi.a = 'hi';
foo(hi.bind(hi)); // hi
this
or that
var Gaya = {
hobby: 'Whisky',
sayHobby: function () {
console.log(this.hobby);
},
sayHobbyAfterAFewDrinks: function () {
setTimeout(function () {
console.log(this.hobby);
}, 1000);
},
};
Gaya.sayHobby(); // Whisky
Gaya.sayHobbyAfterAFewDrinks(); // undefined
this
or that
var Gaya = {
hobby: 'Whisky',
sayHobby: function () {
console.log(this.hobby);
},
sayHobbyAfterAFewDrinks: function () {
var that = this;
setTimeout(function () {
console.log(that.hobby);
}, 1000);
},
};
Gaya.sayHobby(); // Whisky
Gaya.sayHobbyAfterAFewDrinks(); // Whisky
this
or that
var Gaya = {
hobby: 'Whisky',
sayHobby: function () {
console.log(this.hobby);
},
sayHobbyAfterAFewDrinks: function () {
setTimeout(function () {
console.log(this.hobby);
}.bind(this), 1000);
},
};
Gaya.sayHobby(); // Whisky
Gaya.sayHobbyAfterAFewDrinks(); // Whisky
this
or that
var Gaya = {
hobby: 'Whisky',
sayHobby: function () {
console.log(this.hobby);
},
sayHobbyAfterAFewDrinks: function () {
setTimeout(() => {
console.log(this.hobby);
}, 1000);
},
};
Gaya.sayHobby(); // Whisky
Gaya.sayHobbyAfterAFewDrinks(); // Whisky
var Car = {
sound: 'Vroom vroom',
drive: function () {
console.log(this.sound);
}
};
var Bus = {
sound: 'Rumble rumble',
drive: Car.drive,
};
Bus.drive(); // Rumble rumble
function superHardMathProblem(callback) {
console.log('Thinking...');
setTimeout(callback, 1000);
}
superHardMathProblem(function () {
console.log('Done!');
});
// do other stuff
setTimeout(function() {
setTimeout(function() {
setTimeout(function() {
setTimeout(function() {}, 100);
}, 100);
}, 100);
}, 100);
function wait(cb) {
setTimeout(cb, 100);
}
wait(wait(wait(function () {
console.log('Waited for 300ms!');
})));
function step1() {
setTimeout(step2, 100);
}
function step2() {
setTimeout(step3, 100);
}
function step3() {
console.log('I am done!');
}
step1();
var step1 = () =>
new Promise(resolve => setTimeout(resolve, 100));
var step2 = new Promise(function (resolve) {
setTimeout(resolve, 100);
});
var step3 = new Promise(function () {
return 'I am done!';
});
step1().then(step2).then(step3)
.then(console.log).catch(console.error); // I am done!
Promise.all()
Promise.race()
Promise.resolve()
function StrangeThing() {
// ... some constructor
}
StrangeThing.prototype.upsideDown = function () {
// ... some action
}
console.log(StrangeThing);
// function StrangeThing()
var instance = new StrangeThing();
console.log(instance);
// Object { }
class
?
class StrangeThing {
upsideDown() {
// some action
}
}
console.log(StrangeThing);
// function StrangeThing()
var instance = new StrangeThing();
console.log(instance);
// Object { }
var Foo = {
a: 'World',
bar: function() {
console.log(this.a);
},
};
var Whoop = Object.create(Foo);
Whoop.a = 'Hello',
Whoop.bar(); // Hello
Foo.bar = function () { console.log('Fooled you!'); };
Whoop.bar(); // Fooled you!
var Foo = {
a: 'World',
bar: function() {
console.log(this.a);
},
};
var Whoop = Object.assign({}, Foo, { a: 'Hello' });
Whoop.bar(); // Hello
Foo.bar = function () {};
Whoop.bar(); // Hello
require
vs import
// someModule.js
module.exports = function someModule(input) {
return input + 1;
}
// other file
var someModule = require('./someModule');
someModule(41); // 42
// someModule.js
export default function someModule(input) {
return input + 1;
}
// other file
import someModule from './someModule';
someModule(41); // 42
// someModule.js
export function addTwo(input) {
return input + 2;
}
export function addOne(input) {
return input + 1;
}
// other file
import { addOne, addTwo } from './someModule';
addOne(addTwo(39)); // 42
import('./someModule')
.then(({ addOne, addTwo}) => {
addOne(addTwo(39)); // 42
});
var returnFirstArg = param => param;
returnFirstArg(42, 'Hi', 13); // 42
var noArgs = () => 'Test';
noArgs(); // Test
var withArgs = (a, b, c, d) => a + b + c + d;
withArgs(12, 20, 8, 2); // 42
var withBody = () => {
var something = 'Something';
return something;
};
const
and let
const hello = 'Hi';
hello = 'Overwrite';
// TypeError: invalid assignment to const `hello'
let hi = 'Hello';
if (hi === 'Hello') {
let hi = 'World';
hi; // World
}
hi; // Hello
const firstName = 'Gaya';
const surName = 'Kessler';
const fullName = `${firstName} ${surName}`;
// Gaya Kessler
const multiline = `Well,
this is new!
Awesome.`;
const showName = true;
const expressions =
`Hello ${showName ? firstName : 'you'}!`;
// Hello Gaya
const Gaya = { name: 'Gaya', height: 184, age: 30 };
const { age, name } = Gaya;
const Gaya = ['Gaya', 184, 30];
const [name, height, age] = Gaya;
const Gaya = { name: 'Gaya', height: 184, age: 30 };
const Harry = { ...Gaya, name: 'Harry' };
// { name: 'Harry', height: 184, age: 30 };
// same as
const Harry = Object.assign({}, Gaya, { name: 'Harry' });
const numbers = [1, 2, 3, 4, 5];
const floats = [0.2, 0.5, 3.72];
numbers.concat(floats);
// or
[...numbers, ...floats];
// [1, 2, 3, 4, 5, 0.2, 0.5, 3.72]
[...numbers, 6, 7, 8, 9, 10];
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
async await
async function fetchContent() {
try {
const result = await fetch('https://www.google.com');
const text = await result.text();
return text;
} catch (e) {
throw new Error(e.message);
}
}
const text = await fetchContent();
this
altogethermap reduce filter find
function compose(...fns) {
const reversedFns = [...fns].reverse();
return (input) =>
reversedFns.reduce((value, fn) => fn(value), input);
}
const roundString = compose(Math.round, parseFloat);
roundString('80.5'); // 81
const canLoose = (obj) => ({
...obj,
loose() { console.log(`${this.name} lost!`); }
});
const canWin = (obj) => ({
...obj,
win() { console.log(`${this.name} won!`); }
});
function createGame(input) {
return canLoose(canWin(input));
// or
return compose(canLoose, canWin)(input);
}