Mixins
A mixin is reusable behaviour you copy or apply onto a class, because JavaScript classes can extend only one parent. The class has the behaviour; it does not become a second type.
Detailed Explanation
extends is a single chain. When Admin needs User identity and also "can emit events" and "can be logged", you cannot write two parents. A mixin is a function or object that adds those methods to a class.
The usual form is a function that takes a class and returns a subclass:
const Evented = (Base) =>
class extends Base {
on(name, fn) {
/* ... */
}
emit(name, data) {
/* ... */
}
};
class Admin extends Evented(User) {}
Admin still extends one class — the anonymous class Evented returned — and that class extends User. You can stack Logged(Evented(User)). Each layer is a real constructor in the chain. instanceof against User still works. There is no instanceof Evented, because Evented is a function, not a type.
A simpler form copies methods onto a prototype:
Object.assign(Admin.prototype, eventMethods);
That shares functions and does not create intermediate constructors. super inside the copied methods will be wrong if those methods used super, because their home object did not change the way you think. Copy only plain methods that use this, not super.
A third form is an object you delegate to: this.events = createEmitter(). That is composition. It is often clearer than a mixin. The mixin wins when the methods should appear on the instance (admin.on(...)) and several classes should look the same to callers.
Name collisions are your problem. Two mixins that both define on silently overwrite. Order of application is the order of the chain: the leftmost wrapper in A(B(C)) is the most derived.
What it costs you: opacity. The prototype chain grows. Stack traces show anonymous classes unless you name them (class Evented extends Base). Tools and docs have to explain that Admin is a User plus extras. TypeScript can model mixins, but the types get heavy.
Reach for a mixin when several unrelated classes need the same small protocol and callers should call it as methods. Reach for composition when the extra behaviour is a separate object with its own lifecycle. Reach for a single well-named base class when every subtype truly is that type.
Code Examples
const Timestamped = (Base) =>
class Timestamped extends Base {
createdAt = Date.now();
ageMs(now = Date.now()) {
return now - this.createdAt;
}
};
class Entity {
constructor(id) {
this.id = id;
}
}
class Order extends Timestamped(Entity) {
constructor(id, total) {
super(id);
this.total = total;
}
}
const order = new Order("o-1", 50);
console.log(order.id); // => o-1
console.log(typeof order.ageMs); // => function
console.log(order instanceof Entity); // => trueconst speak = {
greet() {
return `hi ${this.name}`;
},
};
class Person {
constructor(name) {
this.name = name;
}
}
Object.assign(Person.prototype, speak);
console.log(new Person("Ada").greet()); // => hi AdaCommon Gotchas
Mixins are not types
`instanceof Evented` does not work when `Evented` is a function. Document the protocol, or add a brand field.
Method name clashes overwrite
apply mixins in a deliberate order, or namespace methods (`eventsOn`) if two sources share a verb.
Do not copy methods that use `super`
`Object.assign` keeps the original home object. Prefer the subclass-factory mixin form.
Each factory mixin adds a constructor to the chain
deep stacks make `super()` argument plumbing harder. Keep the stack short.