Eye Training · Functions, this and Closures
this is decided by how a function is called, not where it's written. A closure keeps whatever it can see, for as long as it lives.
Part 2 · Surface 22. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.
J · The basics
Bug hunt
J · Click counter
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Stopwatch
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Line totals
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Map to ids
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Health route
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Add to a list
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
M · Three years in, these should jump out
Bug hunt
M · Search box
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Format prices
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Destructured service
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Connect once
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Toggle button
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Counter factory
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
S · Where seniors stand out
Bug hunt
S · Export summaries
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Deep clone
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
What your eye checks
Whenever a function is passed around, stored, or defined inside another, ask:
- How will it be called? Passed as a callback (
addEventListener,map,setTimeout) or destructured, a method loses its object. - Arrow or
function? Arrows takethisfrom where they're written: right for callbacks inside methods, wrong as object methods. - Does the arrow body return?
{ }needsreturn; an object literal needs({ }). - Is it defined before it's used?
constandlethave a temporal dead zone;functiondeclarations are hoisted. - Is a stateful function created once or on every call? Debounced, throttled and memoized functions must be created once.
- What does the closure capture, and how long does it live? A long-lived callback keeps every variable it refers to.
- What happens on the second call?
once, memoize and init helpers must return the stored result. - Does recursion have a way out? Base cases, depth limits, cycles.
Tells
| You see | Ask |
|---|---|
addEventListener('…', obj.method) | What is this? |
method: () => { this… } in an object | Arrow as a method? |
.map((x) => { | Is there a return? |
=> { key: value } | Block or object? |
| A route using a handler defined below it | const or function? |
debounce(fn, ms)(…) inside a handler | Created on every call? |
.map(this.method) | Detached method? |
const { method } = service | Detached method? |
this. inside an arrow inside a class | Which this? |
A closure stored in a long-lived Map | What does it keep alive? |
| A hand-written deep clone | Dates? Cycles? |
Go deeper
- Callbacks Done Wrong:
this, callbacks and the habits that cause these bugs - Polyfills: writing
bind,once,debounceandmemoizecorrectly - Previous surface: Loops and iteration · Next: Time and dates