29 Feb 2020
—
JavaScript,
Design Pattern,
Performance
Using a generator function’s yield
we can segment a long running process into small chunks that fit the Idle Until Urgent pattern.
const promise = new IdlePromise(function* (resolve, reject) {
chunkA()
yield
chunkB()
yield
chunkC()
resolve()
})
Read more
02 Dec 2019
—
JavaScript,
Methods
You can customize all the ways objects are manipulated with Proxy
, and still use the built in object prototype methods with Reflect
.
const proxy = new Proxy(object, Reflect)
Read more
01 Dec 2019
—
JavaScript,
Methods
Proxy
allows you to intercept all object prototype methods.
new Proxy(object, {
get: (obj, key) => { return 42 }
}) // the answer to everything is 42
Read more
30 Nov 2019
—
JavaScript
Just a simple way to conditionnaly add a key-value pair at object declaration.
const obj = {
...(flag && {key: value})
}
Read more
29 Nov 2019
—
JavaScript,
Design Pattern,
Closures,
Good practices
When only a small portion of your function needs to change, try to encapsulate it one level of abstraction above.
const getNavTo = index => () => {
navigateTo(index)
}
document.querySelectorAll('.tab').forEach(($el, index) => {
$el.addEventListener('click', getNavTo(index))
})
Read more
28 Nov 2019
—
CSS,
Performance
The contain
CSS property allows you to define an element as a style boundary in order to optimize the browser’s paints, layouts, composite, and style contexts calculations.
Read more
03 Aug 2019
—
JavaScript,
HTML,
Good practices
DOM node IDs are directly available in the window
object.
console.log(window.foo) // HTMLElement
Though, you shouldn’t access the DOM that way.
Read more
02 Aug 2019
—
JavaScript,
Web API,
Design pattern
We can create our own event types, listen to and emmit them from anywhere, and implement event driven behaviors.
$el.addEventListener('foo', () => {
console.log('received foo event')
})
$el.dispatchEvent(new CustomEvent('foo'))
// 'received foo event'
Read more
01 Aug 2019
—
JavaScript,
Design pattern
JavaScript allows you to intercept when an object property is accessed or assigned to.
const foo = {
bar: 42,
get bar() { return this.bar + 1 }
}
console.log(foo.bar) // 43
Read more
31 Jul 2019
—
JavaScript,
Design pattern,
Promises
Any object containing a then()
property — also called a thenable — can be turned into a promise.
const foo = {
then: fn => fn(42)
}
Promise.resolve(foo).then(console.log) // 42
Read more
30 Jul 2019
—
JavaScript,
Primitives
Logical operators ||
and &&
can return non boolean values after internal boolean operations have resolved. Type coercion is only internal.
const foo = true && 'foo' // 'foo'
const bar = false || 'bar' // 'bar'
Read more
17 Jul 2019
—
JavaScript,
Methods
Object.assign()
is a method that allows you to easily merge, override, clone, default object properties.
const foo = {foo: 'foo'}
Object.assign(foo, {bar: 'bar'})
console.log(foo.bar) // 'bar'
Read more
15 Jul 2019
—
JavaScript,
Design pattern,
Web API,
Performance
Defer calculation of costly values to idle periods, but compute immediately if it is needed before that.
let foo = {
get value() {
if (this._value)
return this._value
cancelIdleCallback(idleHandle)
return costly()
}
}
const idleHandle = requestIdleCallback(() => foo._value = costly())
Read more
12 Jul 2019
—
JavaScript,
Anonymous functions,
Good practices
You don’t need to declare an anonymous function to use as an argument.
// Don't
functionCallback(value => console.log(value))
// Do
functionCallback(console.log)
Read more
10 Jul 2019
—
JavaScript,
Methods,
Good practices,
Performance
We don’t often see addEventListener()
be used with a third argument, but it allows for great preformance improvements and cleaner code.
window.addEventListener('scroll', () => {}, {passive: true})
$el.addEventListener('click', () => {}, {once: true})
Read more
08 Jul 2019
—
JavaScript,
Design pattern,
Performance
Compute a value only when needed, but then only once.
get bar() {
const bar = getBar()
Object.defineProperty(this, 'bar', { value: bar })
return bar
}
Read more