Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Specify questions related to Mixins – is a generic object-oriented programming term: a class that contains methods for other classes.

In JavaScript we can only inherit from a single object. There can be only one [[Prototype]] for an object. And a class may extend only one other class.

But sometimes that feels limiting. For instance, I have a class StreetSweeper and a class Bicycle, and want to make a StreetSweepingBicycle.

Or, talking about programming, we have a class Renderer that implements templating and a class EventEmitter that implements event handling, and want to merge these functionalities together with a class Page, to make a page that can use templates and emit events.

There’s a concept that can help here, called “mixins”.

As defined in Wikipedia, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes.

In other words, a mixin provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.

Reference: https://javascript.info/mixins