Js mixin pattern

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

Js mixin pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Add reusable behaviors to classes without deep inheritance chains

How to use this skill

Skill file

JS Mixin Pattern

Add reusable behaviors to classes without deep inheritance chains

When to Use

Instructions

  1. Define a mixin as a function that takes a superclass and returns a new class extending it.
  2. Apply mixins by chaining: class MyClass extends Mixin2(Mixin1(Base)) {}.
  3. Keep mixins focused on a single capability — avoid fat mixins that do too much.
  4. Alternatively, use Object.assign(Target.prototype, mixinMethods) for simpler method injection.
// Functional mixin approach
const Serializable = (Base) =>
  class extends Base {
    serialize() {
      return JSON.stringify(this);
    }
    static deserialize(json) {
      return Object.assign(new this(), JSON.parse(json));
    }
  };

const Timestamped = (Base) =>
  class extends Base {
    constructor(...args) {
      super(...args);
      this.createdAt = new Date();
    }
  };

class User extends Serializable(Timestamped(class {})) {
  constructor(name) {
    super();
    this.name = name;
  }
}

const u = new User('Alice');
console.log(u.serialize()); // {"name":"Alice","createdAt":"..."}
  1. Use TypeScript intersection types to type mixed-in methods correctly.

Details

JavaScript's single-prototype-chain inheritance means a class can only extend one parent. Mixins work around this by composing behaviors through function application rather than inheritance.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/mixin-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.