Agent SkillsPatternsDev/skills › mixin-pattern

mixin-pattern

GitHub

介绍 Mixin 模式,用于在不使用继承的情况下向多个类或对象添加可复用行为。通过 Object.assign 修改原型实现功能组合,同时提醒注意原型污染风险及 React Hooks 的替代方案。

javascript/mixin-pattern/SKILL.md PatternsDev/skills

Trigger Scenarios

需要向多个不共享祖先的类添加通用功能 希望避免深层继承链而采用行为组合

Install

npx skills add PatternsDev/skills --skill mixin-pattern -g -y
More Options

Non-standard path

npx skills add https://github.com/PatternsDev/skills/tree/main/javascript/mixin-pattern -g -y

Use without installing

npx skills use PatternsDev/skills@mixin-pattern

指定 Agent (Claude Code)

npx skills add PatternsDev/skills --skill mixin-pattern -a claude-code -g -y

安装 repo 全部 skill

npx skills add PatternsDev/skills --all -g -y

预览 repo 内 skill

npx skills add PatternsDev/skills --list

SKILL.md

Frontmatter
{
    "name": "mixin-pattern",
    "paths": [
        "**\/*.js",
        "**\/*.ts"
    ],
    "license": "MIT",
    "metadata": {
        "author": "patterns.dev",
        "version": "1.1"
    },
    "description": "Teaches the mixin pattern for sharing functionality without inheritance. Use when you need to add reusable behavior to multiple objects or classes that don't share a common ancestor.",
    "related_skills": [
        "module-pattern",
        "singleton-pattern"
    ]
}

Mixin Pattern

A mixin is an object that we can use in order to add reusable functionality to another object or class, without using inheritance. We can't use mixins on their own: their sole purpose is to add functionality to objects or classes without inheritance.

Let's say that for our application, we need to create multiple dogs. However, the basic dog that we create doesn't have any properties but a name property.

When to Use

  • Use this when you need to add reusable functionality to multiple classes without creating an inheritance chain
  • This is helpful when you want to compose behavior from multiple sources

When NOT to Use

  • When composition via hooks (React) or composables (Vue) achieves the same result with better traceability
  • When prototype pollution is a risk — mixins modify shared prototypes and can cause naming collisions
  • When the added functionality is simple enough that a utility function or module import suffices

Instructions

  • Use Object.assign() to add mixin properties to a class prototype
  • Be cautious with prototype pollution — modifying prototypes can lead to unexpected behavior
  • In React, prefer Hooks over mixins (mixins are discouraged by the React team)
  • Consider composition over inheritance when designing reusable behavior

Details

class Dog {
  constructor(name) {
    this.name = name;
  }
}

A dog should be able to do more than just have a name. It should be able to bark, wag its tail, and play! Instead of adding this directly to the Dog, we can create a mixin that provides the bark, wagTail and play property for us.

const dogFunctionality = {
  bark: () => console.log("Woof!"),
  wagTail: () => console.log("Wagging my tail!"),
  play: () => console.log("Playing!"),
};

We can add the dogFunctionality mixin to the Dog prototype with the Object.assign method. This method lets us add properties to the target object: Dog.prototype in this case. Each new instance of Dog will have access to the properties of dogFunctionality, as they're added to the Dog's prototype!

class Dog {
  constructor(name) {
    this.name = name;
  }
}

const dogFunctionality = {
  bark: () => console.log("Woof!"),
  wagTail: () => console.log("Wagging my tail!"),
  play: () => console.log("Playing!"),
};

Object.assign(Dog.prototype, dogFunctionality);

Let's create our first pet, pet1, called Daisy. As we just added the dogFunctionality mixin to the Dog's prototype, Daisy should be able to walk, wag her tail, and play!

const pet1 = new Dog("Daisy");

pet1.name; // Daisy
pet1.bark(); // Woof!
pet1.play(); // Playing!

Perfect! Mixins make it easy for us to add custom functionality to classes or objects without using inheritance.

Although we can add functionality with mixins without inheritance, mixins themselves can use inheritance!

Most mammals can walk and sleep as well. A dog is a mammal, and should be able to walk and sleep!

Let's create a animalFunctionality mixin that adds the walk and sleep properties.

const animalFunctionality = {
  walk: () => console.log("Walking!"),
  sleep: () => console.log("Sleeping!"),
};

We can add these properties to the dogFunctionality prototype, using Object.assign. In this case, the target object is dogFunctionality.

const animalFunctionality = {
  walk: () => console.log("Walking!"),
  sleep: () => console.log("Sleeping!"),
};

const dogFunctionality = {
  bark: () => console.log("Woof!"),
  wagTail: () => console.log("Wagging my tail!"),
  play: () => console.log("Playing!"),
  walk() {
    super.walk();
  },
  sleep() {
    super.sleep();
  },
};

Object.assign(dogFunctionality, animalFunctionality);
Object.assign(Dog.prototype, dogFunctionality);

Perfect! Any new instance of Dog can now access the walk and sleep methods as well.

An example of a mixin in the real world is visible on the Window interface in a browser environment. The Window object implements many of its properties from the WindowOrWorkerGlobalScope and WindowEventHandlers mixins, which allow us to have access to properties such as setTimeout and setInterval, indexedDB, and isSecureContext.

Since it's a mixin, thus is only used to add functionality to objects, you won't be able to create objects of type WindowOrWorkerGlobalScope.

React (pre ES6)

Mixins were often used to add functionality to React components before the introduction of ES6 classes. The React team discourages the use of mixins as it easily adds unnecessary complexity to a component, making it hard to maintain and reuse. The React team encouraged the use of higher order components instead, which can now often be replaced by Hooks.

Mixins allow us to easily add functionality to objects without inheritance by injecting functionality into an object's prototype. Modifying an object's prototype is seen as bad practice, as it can lead to prototype pollution and a level of uncertainty regarding the origin of our functions.

Source

References

Version History

  • 48bf58a Current 2026-07-24 16:34

Same Skill Collection

javascript/bundle-splitting/SKILL.md
javascript/command-pattern/SKILL.md
javascript/compression/SKILL.md
javascript/dynamic-import/SKILL.md
javascript/factory-pattern/SKILL.md
javascript/flyweight-pattern/SKILL.md
javascript/import-on-interaction/SKILL.md
javascript/import-on-visibility/SKILL.md
javascript/islands-architecture/SKILL.md
javascript/js-performance-patterns/SKILL.md
javascript/loading-sequence/SKILL.md
javascript/mediator-pattern/SKILL.md
javascript/module-pattern/SKILL.md
javascript/observer-pattern/SKILL.md
javascript/prefetch/SKILL.md
javascript/preload/SKILL.md
javascript/prototype-pattern/SKILL.md
javascript/provider-pattern/SKILL.md
javascript/proxy-pattern/SKILL.md
javascript/prpl/SKILL.md
javascript/route-based/SKILL.md
javascript/singleton-pattern/SKILL.md
javascript/static-import/SKILL.md
javascript/third-party/SKILL.md
javascript/tree-shaking/SKILL.md
javascript/view-transitions/SKILL.md
javascript/virtual-lists/SKILL.md
javascript/vite-bundle-optimization/SKILL.md
react/ai-ui-patterns/SKILL.md
react/client-side-rendering/SKILL.md
react/compound-pattern/SKILL.md
react/hoc-pattern/SKILL.md
react/hooks-pattern/SKILL.md
react/incremental-static-rendering/SKILL.md
react/presentational-container-pattern/SKILL.md
react/progressive-hydration/SKILL.md
react/react-2026/SKILL.md
react/react-composition-2026/SKILL.md
react/react-data-fetching/SKILL.md
react/react-render-optimization/SKILL.md
react/react-selective-hydration/SKILL.md
react/react-server-components/SKILL.md
react/render-props-pattern/SKILL.md
react/server-side-rendering/SKILL.md
react/static-rendering/SKILL.md
react/streaming-ssr/SKILL.md
vue/async-components/SKILL.md
vue/components/SKILL.md
vue/composables/SKILL.md

Metadata

Files
0
Version
48bf58a
Hash
4c73aac0
Indexed
2026-07-24 16:34

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-20 05:43
浙ICP备14020137号-1 $mapa de visitantes$