
https://patterns-dev-kr.github.io/design-patterns/mixin-pattern/

구성 요소
동작 방식
Object.assign(target, mixin) 또는 Object.assign(Class.prototype, mixin)을 통해 mixin 객체의 메서드를 타깃에 복사한다.장점
단점
class Dog {
constructor(name) {
this.name = name;
}
}
const dogFunctionality = {
bark() {
console.log("Woof!");
},
wagTail() {
console.log("Wagging my tail!");
},
play() {
console.log("Playing!");
},
};
Object.assign(Dog.prototype, dogFunctionality);
const pet = new Dog("Daisy");
pet.bark(); // Woof!
pet.play(); // Playing!
const CanSwim = {
swim() {
console.log(`${this.name} is swimming.`);
},
};
const CanJump = {
jump() {
console.log(`${this.name} is jumping.`);
},
};
const fish = { name: "Goldfish" };
Object.assign(fish, CanSwim, CanJump);
fish.swim(); // Goldfish is swimming.
fish.jump(); // Goldfish is jumping.
const mixin = {
get random() {
return Math.random();
},
};
const obj = {};
Object.assign(obj, mixin);
console.log(obj.random); // 0.5342...
console.log(obj.random); // 0.9123...