06. Mixins

One way of implementing a mixin in ES6 is to view it as a function whose input is a superclass and whose output is a subclass extending that superclass:

const Storage = Sup => class extends Sup {
    save(database) { ··· }
};
const Validation = Sup => class extends Sup {
    validate(schema) { ··· }
};

// With these mixins, Employee is created like this:
class Employee extends Storage(Validation(Person)) { ··· }

Here, we profit from the operand of the extends clause not being a fixed identifier, but an arbitrary expression.

Last updated