The Decorator pattern wraps an object or function to add behaviour without changing the original or creating a subclass for every combination. The wrapper exposes the same interface, does something extra (logging, caching, retries, auth checks), and delegates to the wrapped thing. In JavaScript the most common form is the higher-order function (withRetry(fetchUser)), with higher-order components in React and the TC39 decorator syntax (@logged) for classes, which TypeScript 5+ supports and engines are implementing.
function withLogging(fn, label = fn.name) {
return function (...args) {
console.time(label);
try {
return fn.apply(this, args);
} finally {
console.timeEnd(label);
}
};
}
function memoize(fn) {
const cache = new Map();
return (arg) => {
if (!cache.has(arg)) cache.set(arg, fn(arg));
return cache.get(arg);
};
}
function withRetry(fn, { attempts = 3, delayMs = 200 } = {}) {
return async (...args) => {
for (let i = 1; ; i++) {
try {
return await fn(...args);
} catch (err) {
if (i >= attempts) throw err;
await new Promise((r) => setTimeout(r, delayMs * 2 ** (i - 1)));
}
}
};
}
// decorators compose
const getUser = withRetry(withLogging(fetchUser), { attempts: 4 });
Each decorator handles one concern; you stack them as needed instead of building RetryingLoggingCachedFetcher.
Wrap an object and forward the interface:
function withCache(storage) {
const memory = new Map();
return {
...storage,
async get(key) {
if (memory.has(key)) return memory.get(key);
const value = await storage.get(key);
memory.set(key, value);
return value;
},
};
}
A Proxy can decorate every method generically (e.g. tracing all calls on an API client).
const withAuth = (Component) => function Authed(props) {
const user = useUser();
return user ? <Component {...props} user={user} /> : <Navigate to="/login" />;
};
export default withAuth(Dashboard);
HOCs are decorators for components. Hooks replaced many of them, because stacking HOCs creates "wrapper hell" and prop-name collisions, but they're still used for cross-cutting concerns (error boundaries, memo, forwardRef in older code).
function logged(method, context) {
return function (...args) {
console.log(`→ ${String(context.name)}`, args);
return method.apply(this, args);
};
}
class Cart {
@logged
add(item) { /* … */ }
}
The standard decorator receives the value and a context object (kind, name, addInitializer, …). It's different from the older "experimental" decorators (TypeScript experimentalDecorators, Babel legacy) used by Angular, NestJS and MobX 5, so check which semantics a framework expects. Native engine support is still rolling out, so you typically compile them with TypeScript or Babel.
this and metadata: forward this with apply, and be aware that wrappers change fn.name and fn.length (set them explicitly if anything depends on them).withRetry(withCache(f)) vs withCache(withRetry(f)) behave differently.Authed).this forwarding and the difference from Proxy/Adapter.Spotted a mistake or a rights concern? Report an issue