Mixins

This lesson teaches the concept of mixins in JavaScript, their syntax, and implementation using an example.

We'll cover the following

What are Mixins?

So far you’ve learned that for a class to call methods from another class it needs to inherit those methods first. Thus the child class extends the parent class, inherits its methods, and then invokes them. However, there is a limiting factor; a class can only inherit from one other class only.

That brings us to the question.

Is there a class whose methods can be inherited by other classes without it having to be their parent class? This is where mixins are implemented.

A mixin is a class that contains various methods that implement different functionalities. Other classes can then inherit those methods without the mixin class having to be their parent class.

Note: A mixin class is not used alone. It only provides other classes with extra methods.

Syntax

Let’s take a look at the syntax to implement a mixin:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy