Skip to content
Back to skills

Js Decorator Pattern

ASecurity

> Extend object behavior dynamically without modifying its source

  • 20 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 11, 2026
toolsjavascriptjavaexpress

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill js-decorator-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js Decorator Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js Decorator Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-js-decorator-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-js-decorator-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# JS Decorator Pattern

> 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

1. Write a decorator function that accepts a target function or object and returns an enhanced version.
2. For class decorators (TC39 Stage 3): prefix the class or method declaration with `@decorator`.
3. For functional decorators: wrap the original function — `const enhanced = decorate(original)`.
4. The decorator should not modify the original — it returns a new function/object.

```javascript
// 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
```

5. Stack decorators by composing them: `const fn = withAuth(withLogging(handler))`.
6. 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

## Source

https://patterns.dev/javascript/decorator-pattern

## Process

1. Read the instructions and examples in this document.
2. Apply the patterns to your implementation, adapting to your specific context.
3. 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.

Files in this skill

  • SKILL.md2.8 KB
  • skill.yaml650 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…