Mixin

Noun · Development

Definitions

  1. A class or module that supplies methods to other classes for reuse without being intended to stand alone or serve as their parent in a conceptual is-a hierarchy. The idea comes from the Flavors object system for Lisp in the late 1970s and early 1980s, and the name is usually traced to mix-in toppings at an ice-cream shop near MIT. In Ruby, modules brought in with include or extend are the standard mixin mechanism, as with Comparable and Enumerable, which grant many methods once a class defines a single one. Python achieves the same with multiple inheritance by convention, Scala uses traits, and TypeScript and JavaScript apply mixins by composing class factories. The trade-off is that methods arrive from several sources, so name collisions and resolution order must be understood, which is why React retired its mixins in favour of higher-order components and later hooks. Sass uses @mixin in a separate sense, for reusable blocks of CSS.

    In plain English: A chunk of reusable code you can plug into any class to give it extra abilities, without making it inherit from a specific parent.

    Example: "Add the Serializable mixin and your class instantly knows how to convert itself to JSON."

Related Terms

Collections Including This Term