Js decorator pattern

From Intense-Visions/harness-engineering · View interactive version

Js decorator pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Extend object behavior dynamically without modifying its source

How to use this skill

Skill file

JS Decorator Pattern

Extend object behavior dynamically without modifying its source

When to Use

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.
// 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
  1. Stack decorators by composing them: const fn = withAuth(withLogging(handler)).
  2. 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:

When NOT to use:

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

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.