Extend object behavior dynamically without modifying its source
When to Use
You need to add responsibilities to objects at runtime without subclassing
You want to compose behaviors (logging, caching, auth checks) around existing functions
Building higher-order functions that wrap existing logic with cross-cutting concerns
Instructions
Write a decorator function that accepts a target function or object and returns an enhanced version.
For class decorators (TC39 Stage 3): prefix the class or method declaration with @decorator.
For functional decorators: wrap the original function — const enhanced = decorate(original).
The decorator should not modify the original — it returns a new function/object.
// Functional decorator — adds logging
function withLogging(fn) {
return function (...args) {
console.log(`Calling ${fn.name} with`, args);
const result = fn.apply(this, args);
console.log(`${fn.name} returned`, result);
return result;
};
}
function add(a, b) {
return a + b;
}
const loggedAdd = withLogging(add);
loggedAdd(2, 3); // logs call and return
Stack decorators by composing them: const fn = withAuth(withLogging(handler)).
Preserve the original function's name and length with Object.defineProperty if needed.
Details
The Decorator pattern adds behavior to individual objects without affecting other objects of the same class. In JavaScript, this is most naturally expressed as higher-order functions — functions that take a function and return a new function with added behavior.
Trade-offs:
Stacking many decorators makes stack traces deeper and harder to debug
Decorated functions lose the original's .name and .length unless explicitly preserved
Order of decoration matters — withAuth(withLogging(fn)) logs before auth checks
When NOT to use:
When the added behavior is always needed — just include it in the function directly
When a simple wrapper function is sufficient — do not over-engineer with a formal decorator abstraction
For complex object augmentation — consider mixins or composition instead
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-decorator-pattern3description: JS Decorator Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/js-decorator-pattern6---7# JS Decorator Pattern89> Extend object behavior dynamically without modifying its source1011## When to Use1213- You need to add responsibilities to objects at runtime without subclassing14- You want to compose behaviors (logging, caching, auth checks) around existing functions15- Building higher-order functions that wrap existing logic with cross-cutting concerns1617## Instructions18191. Write a decorator function that accepts a target function or object and returns an enhanced version.202. For class decorators (TC39 Stage 3): prefix the class or method declaration with `@decorator`.213. For functional decorators: wrap the original function — `const enhanced = decorate(original)`.224. The decorator should not modify the original — it returns a new function/object.2324```javascript25// Functional decorator — adds logging26function withLogging(fn) {27 return function (...args) {28 console.log(`Calling ${fn.name} with`, args);29 const result = fn.apply(this, args);30 console.log(`${fn.name} returned`, result);31 return result;32 };33}3435function add(a, b) {36 return a + b;37}38const loggedAdd = withLogging(add);39loggedAdd(2, 3); // logs call and return40```41425. Stack decorators by composing them: `const fn = withAuth(withLogging(handler))`.436. Preserve the original function's `name` and `length` with `Object.defineProperty` if needed.4445## Details4647The Decorator pattern adds behavior to individual objects without affecting other objects of the same class. In JavaScript, this is most naturally expressed as higher-order functions — functions that take a function and return a new function with added behavior.4849**Trade-offs:**5051- Stacking many decorators makes stack traces deeper and harder to debug52- Decorated functions lose the original's `.name` and `.length` unless explicitly preserved53- Order of decoration matters — `withAuth(withLogging(fn))` logs before auth checks5455**When NOT to use:**5657- When the added behavior is always needed — just include it in the function directly58- When a simple wrapper function is sufficient — do not over-engineer with a formal decorator abstraction59- For complex object augmentation — consider mixins or composition instead6061## Source6263https://patterns.dev/javascript/decorator-pattern6465## Process66671. Read the instructions and examples in this document.682. Apply the patterns to your implementation, adapting to your specific context.693. Verify your implementation against the details and edge cases listed above.7071## Harness Integration7273- **Type:** knowledge — this skill is a reference document, not a procedural workflow.74- **No tools or state** — consumed as context by other skills and agents.7576## Success Criteria7778- The patterns described in this document are applied correctly in the implementation.79- Edge cases and anti-patterns listed in this document are avoided.
Run npx get-skill add intense-visions/js-decorator-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
JS Decorator Pattern It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.