Joined December 2024
useless_engineer.dev retweeted
const cfg = Object.freeze({ ui: { dark: false } }); cfg.ui = null; // ignored cfg.ui.dark = true; // works freeze is shallow: the top level is locked, nested objects still change. In strict mode the blocked write throws a TypeError. Freeze each level for a deep lock.
1
1
2
17
Object.freeze only locks the top-level properties. A nested object is a separate object nobody froze, so cfg.db.port = 1 still works and the "frozen" config keeps changing underneath you. #javascript #react #typescript instagram.com/reel/De3Uvu0qa…
1
2
30
Object.isFrozen(cfg.db) is false. A recursive deepFreeze locks every level, children first. In TypeScript, as const makes every nested property readonly at compile time, and Immer turns edits into a new object instead of a mutation. 🔖 save slide 5.
1
5
P148: const blocks reassignment of the binding, not mutation of the value it points at. push, splice and property writes still change the same object, and everything holding that reference sees it. #javascript #react #typescript instagram.com/reel/De0e7hzqU…
1
2
38
Object.freeze locks the object itself at runtime, though only one level deep. Or never edit in place: build a new object with spread, and type it Readonly so the compiler rejects writes. 🔖 save slide 5.
1
5
A validation check meant to mean "a plain object" lets arrays and null slip straight through, and the bad value breaks something deep inside instead of at the check. Array.isArray answers the array question directly.
1
1
8
For plain objects, write one helper that rules out null and arrays explicitly, then reuse it everywhere. 🔖 save slide 5.
1
3
useless_engineer.dev retweeted
const { matches } = matchMedia( "(prefers-reduced-motion: reduce)" ); if (!matches) startConfetti(); matchMedia runs a CSS media query from JS. If someone turned on reduced motion in their OS, matches is true, so skip or tone down big animations. Listen for "change" too.
1
2
51
Dump everything into a set instead, and only start counting forward from numbers with no predecessor in the set. O(n log n) becomes O(n).
1
6
useless_engineer.dev retweeted
const a = ["js", "css"]; const b = a.with(1, "scss"); a // ["js", "css"] b // ["js", "scss"] with(index, value) returns a copy with one item swapped and leaves the original untouched, handy for React state. A negative index counts from the end.
1
1
35
P144: delete arr[i] removes the key entirely, leaving a real hole — but .length doesn't shrink, so the array keeps reporting its old count while sparse underneath. #javascript #react instagram.com/reel/DetP3UIqr…
1
2
21
Different methods treat the hole inconsistently: forEach skips it silently, JSON.stringify turns it into null. Set the slot to undefined when the position should stay but not the value — keeps the array dense. Use splice when the element should actually be gone; length shrinks.
1
11
Extracting just the primitive before the closure is created lets the rest of the scope get collected. Or remove the listener once it's no longer needed, breaking the chain. 🔖 save slide 5.
1
3
A listener that only logs bigData.length still keeps the whole million-item array reachable — un-garbage-collectable — as long as the listener is attached, even after the DOM node is removed.
1
11
P140: An IIFE with no return statement evaluates to undefined, like any function. The "private" variables you wrote still exist, just trapped in a closure nothing outside can reach. #javascript #react #typescript instagram.com/reel/Den1IXXKa…
1
2
26
No error at definition time — the crash happens later, at the call site, far from the actual mistake. Returning the public API explicitly is the one line that turns a closure into a module. Real ES modules make a missing export fail immediately and by name. 🔖 save slide 5.
1
8