mixin
A mixin is a reusable bundle of methods that can be added to many classes or objects, so unrelated classes share the same behavior without needing a common parent class.
JavaScript classes use single inheritance. Each class extends at most one superclass, and each object links to at most one prototype. A mixin supplies extra behavior from the side instead. ECMA-262 has no mixin syntax, so mixins are a pattern built from ordinary language features.
Two forms are common. An object mixin is a plain object of methods that Object.assign() copies onto a class’s .prototype property. A subclass factory is a higher-order function that takes a base class and returns a new class that extends it.
The factory form works because extends accepts any expression that evaluates to a constructor, including a function call. Each factory call adds one link to the subclass’s prototype chain, so nesting several calls stacks their methods into one chain:
Example
Say an online store’s Product class and a blog’s Article class share no parent, but both need tags. A withTags() subclass factory adds the same tagging behavior to each one:
> const withTags = (Base) =>
… class extends Base {
… tags = new Set();
… addTag(tag) {
… this.tags.add(tag);
… return this;
… }
… };
undefined
> class Product {
… constructor(name) {
… this.name = name;
… }
… }
undefined
> class Article {}
undefined
> class TaggedProduct extends withTags(Product) {}
undefined
> class TaggedArticle extends withTags(Article) {}
undefined
> new TaggedProduct("Lamp").addTag("sale")
TaggedProduct { name: 'Lamp', tags: Set(1) { 'sale' } }
> new TaggedArticle() instanceof Article
true
Each call to withTags() creates a fresh class that sits between the subclass and its base in the prototype chain. The mixin’s tags field is set after the base constructor runs, so name appears first. The instanceof check still finds the original base class.
An object mixin copies its methods instead of linking them. Here, a serializable object lends its serialize() method to a Point class:
> const serializable = {
… serialize() {
… return JSON.stringify(this);
… },
… };
undefined
> class Point {
… constructor(x, y) {
… this.x = x;
… this.y = y;
… }
… }
undefined
> Object.assign(Point.prototype, serializable)
{ serialize: [Function: serialize] }
> new Point(3, 4).serialize()
'{"x":3,"y":4}'
> Object.hasOwn(Point.prototype, "serialize")
true
The method becomes an own property of Point.prototype, so methods added to serializable later never reach Point. Object.assign() also reads getters instead of copying them, which turns a mixin’s accessor properties into plain values.
Related Resources
Course
JavaScript Prototypes and the Object Model
Port a tool library's kit model from Python to JavaScript, and learn how prototype lookup works, what class syntax guarantees, and how classes join protocols and combine behavior.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 22, 2026