Mixin Pattern

patternsdev/skills/javascript/mixin-pattern

作者 patternsdev48bf58a488cdMIT253 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5个月前更新

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.

AI 生成的概览

讲解 JavaScript 混入模式,用于在不使用继承的情况下为对象和类添加可复用功能。

功能
说明什么是混入,以及如何用 Object.assign 把可复用行为挂载到类原型上。通过完整示例演示,包括把一个混入组合进另一个混入,以及浏览器 Window 的真实案例。同时说明不宜使用混入的场景,例如原型污染风险以及 React 更推荐 Hooks。
适用场景
当需要为多个没有共同祖先的对象或类添加共享行为时使用。也适合在混入、组合、Hooks 与普通工具函数之间做取舍时参考。
运行要求
不需要脚本或工具,仅为讲解性内容。示例面向 JavaScript 或 TypeScript 代码库。

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

js
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.

js
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!

js
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!

js
const pet1 = new Dog("Daisy");
pet1.name; // Daisypet1.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.

js
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.

js
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

来源与署名

来源:patternsdev/skills位于javascript/mixin-pattern提交48bf58a

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架