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