[Design Pattern] Mixin pattern in Javascript
Problem to Solve
Share functionality between classes without using inheritance.
Solution
Create a class containing methods that can be used by other classes and apply it to multiple classes.
Code
let sayHiMixin = {
sayHi() {alert(`Hi, ${this.name}`)}
}
class User {
name;
}
Object.assign(User.prototype, sayHiMixin);
Then all the instance of User
will be able to call the sayHi
method.