Inheritance

Dated May 5, 2024; last modified on Sun, 05 May 2024

Mixins

Introduced to mixins by . Didn’t know that there are passionate advocates for this, e.g., ’s “You can even look at normal subclass inheritance as a degenerate form of mixin inheritance where the superclass is known at class definition time, and there’s only one application of it.”

A mix-in is an abstract subclass. This technique is especially useful in languages where a class can only have a single superclass.

const calculatorMixin = (Base) =>
  class extends Base {
    calc() {}
  };

const randomizerMixin = (Base) =>
  class extends Base {
    randomize() {}
  };

class Foo {}
class Bar extends calculatorMixin(randomizerMixin(Foo)) {}

Folks use mixins to provide a lot of optional features for a class, or use one particular feature in a lot of different classes. One way to think about mixins is a small base type designed to add a small amount of functionality to a type without otherwise affecting that type. Mixins are rarely useful as standalone objects. One can think of it as an interface with an associated implementation.

  1. Mixins – Lit. lit.dev . Accessed May 3, 2024.
  2. extends > Examples > Mix-ins. developer.mozilla.org . Accessed May 5, 2024.
  3. 'Real' Mixins with JavaScript Classes. Justin Fagnani. justinfagnani.com . Dec 21, 2015. Accessed May 5, 2024.
  4. python - What is a mixin and why is it useful? - Stack Overflow. stackoverflow.com . Accessed May 5, 2024.
  5. OOP - Mixin vs inheritance - Stack Overflow. stackoverflow.com . Accessed May 5, 2024.