que-son-los-mixins-en-javascript

What are mixins and how to use them in JavaScript

  • 3 min

Mixins are a design pattern that allows combining multiple sources of functionality into a single object.

Mixins are a way to combine multiple objects into one, allowing the resulting object to have all the properties and methods of the original objects.

In other words, a mixin is simply an object with methods and properties that can be “mixed” with other objects to add additional capabilities.

Unlike traditional inheritance, where a class can extend another, mixins favor composition. They are an approach where functionalities are combined dynamically.

Mixins offer an elegant solution for sharing methods and properties between objects, without the need for complex inheritance.

  • Code Reusability: They allow writing common functionality once and applying it in multiple places.
  • Flexibility: They facilitate combining different sets of functionalities as needed.
  • Extensibility: They allow adding capabilities to existing objects without altering their original structure.

How to Use Mixins in JavaScript

Implementing mixins in JavaScript is a straightforward process. A mixin is essentially an object that defines shared methods or properties, which we copy into another object.

Creating Mixins

To create a mixin in JavaScript, we simply create an object with the methods and properties we want to share:

const mixinGreet = {
  greet() {
    console.log(`Hello, I'm ${this.name}`);
  }
};
Copied!

In this example, mixinGreet is a simple mixin that has a greet() method.

Applying Mixins

To apply a mixin to an object, we can use the Object.assign() function, which copies properties from one object to another:

const person = {
  name: 'John'
};

Object.assign(person, mixinGreet);

person.greet(); // Prints: "Hello, I'm John"
Copied!

In this case, we have applied the mixinGreet mixin to the person object, giving it the ability to call the greet() method.

Although mixins are very useful, when combining several mixins, it is possible to generate conflicts between the names of methods or properties.

// Potential conflict if both mixins have a "calculate" method.
Object.assign(object, mixinOne, mixinTwo);
Copied!

The order in which you apply mixins matters. Properties and methods added by later mixins will overwrite the previous ones (if they have the same name).

Object.assign(object, mixinFirst, mixinSecond);
// mixinSecond overwrites matching properties from mixinFirst.
Copied!

Practical Examples