Simulating Multiple Inheritance
An object can have only ONE prototype. But what if a class needs behavior from several sources? Enter Mixins — plain objects whose methods are copied onto a class prototype.
const Serializable = {
serialize() { return JSON.stringify(this); }
};
const Loggable = {
log(message) { console.log(`[${this.constructor.name}] ${message}`); }
};
class User {
constructor(name) { this.name = name; }
}
// Mix both toolboxes into User
Object.assign(User.prototype, Serializable, Loggable);
const user = new User('Alice');
user.log('logged in'); // [User] logged in
console.log(user.serialize()); // {"name":"Alice"}
Note
Mixins build flexible, composable architectures without deep, rigid inheritance hierarchies.
Important
Watch out for naming conflicts! If two mixins have the same method name, the last one applied wins silently.