Classic design patterns were codified for statically-typed, class-heavy languages, JavaScript's first-class functions and closures make several of them dramatically simpler to implement than the textbook version, and make a few others actively unnecessary.
You are viewing the 40% free preview. Upgrade to DeepFrontend Pro to unlock the complete 35-minute masterclass, live interactive engines, and staff-level interview loops.
โUnder the Hood: Deep architectural internals & edge cases
โInterview Q&A: Senior/Staff model answers & red flags
The beginner framing: a design pattern is a reusable solution to a commonly recurring problem in software design, not a library you import, but a shape of code you can recognize and apply.
The precise mental model: most classic design patterns (from the 1994 "Gang of Four" book) were codified for languages like C++ and Java, where you don't have first-class functions or closures, so achieving things like "private state" or "a function you can pass around" required entire class hierarchies. JavaScript's closures (Closures) and prototypes (Prototypes) make several of these patterns dramatically simpler, and this topic focuses specifically on how they show up in idiomatic JavaScript, not the textbook UML-diagram version.
const CounterModule = (function () { let count = 0; // PRIVATE, unreachable from outside this IIFE's scope return { increment() { return ++count; }, reset() { count = 0; }, };})();CounterModule.increment(); // 1CounterModule.count; // undefined, truly private, not just "by convention"
This is exactly the closure-based privacy pattern from Closures, applied deliberately as a design pattern, and it's the historical reason ES Modules (every file's top-level variables are private by default) feel so natural to JS developers: they formalized a pattern the community had already been hand-rolling for years.
// config.js, this IS the singleton, by virtue of ESM module cachinglet settings = { theme: "dark" };export function getSettings() { return settings; }export function updateSettings(patch) { settings = { ...settings, ...patch }; }
Every import of this module gets the exact same, single instance, ESM's module cache (see Modules) guarantees a file's code runs once, and every importer shares the same live bindings. The textbook Singleton pattern (a class with a private constructor and a static getInstance() method guarding a single instance) solves a problem JavaScript modules already solve for free, writing a class-based Singleton in JS is usually a sign of importing a pattern from another language rather than reaching for the idiomatic tool.
Factory, a function that returns different shapes based on input#
A factory centralizes object creation logic, useful when construction involves real decision-making (which variant to build, what defaults to apply) rather than being a trivial one-liner, where it would just be needless indirection.
Observer / Pub-Sub, the pattern behind every event system you've used#
function createEventBus() { const listeners = new Map(); // event name -> Set of callbacks return { on(event, callback) { if (!listeners.has(event)) listeners.set(event, new Set()); listeners.get(event).add(callback); }, off(event, callback) { listeners.get(event)?.delete(callback); }, emit(event, payload) { listeners.get(event)?.
This is the exact mechanism behind EventTarget/addEventListener in the DOM, Node's EventEmitter, and most state-management libraries at their core, subscribers register interest in named events, and a publisher fires them without needing to know who's listening.
Decorator, wrapping a function to add behavior, without modifying it#
function withLogging(fn) { return function (...args) { console.log(`calling ${fn.name} with`, args); const result = fn(...args); console.log(`${fn.name} returned`, result); return result; };}