A mixin is a class whose methods are intended to be added to (or “mixed in” to) other classes.
AnimalMixin is a function that takes a base class and returns a new class that extends the base class and adds the eats method.
Then Employee extends AnimalMixin(Person) - effectively combining Person and Animal.
Also, note that super refers to the parent class, so calling super.description() in Employee will call description on Person, not Animal.
Example:
class Person { constructor(name, age) { this.name = name; this.age = age; } description() { return `A person named ${this.name} who is ${this.age} years old`; } } const AnimalMixin = Base => class extends Base { eats() { return `${this.name} eats like an animal.`; } }; class Employee extends AnimalMixin(Person) { constructor(name, age, salary, jobTitle) { super(name, age); this.salary = salary; this.jobTitle = jobTitle; } description() { return `An employee is also ${super.description()}`; } } let employee = new Employee("Aurel", 30, 15000, "CEO boss"); console.log(employee.eats()); // Aurel eats. console.log(employee.description()); // An employee is also A person named Aurel who is 30 years old
Same functionality can be achieved using Object.assign approach.
In this case, we're directly adding the methods from AnimalMixin to the prototype of Employee class using Object.assign(). This way, all instances of Employee will have access to the eats method.
let AnimalMixin = { eats() { return `${this.name} eats.`; } }; class Employee { constructor(name, age, salary, jobTitle) { this.name = name; this.age = age; this.salary = salary; this.jobTitle = jobTitle; } description() { return `A person named ${this.name} who is ${this.age} years old`; } } Object.assign(Employee.prototype, AnimalMixin); let employee = new Employee("Aurel", 30, 15000, "CEO boss"); console.log(employee.eats()); // Aurel eats.