To define class in es6, you can do this
class A {
x = 1;
y = 2;
z = 3;
mx () { // ... }
my () { // ... }
mz () { // ... }
}
When there are more and more A class attribute methods, it is found that the entire file has a large amount of code. Can some properties and methods be divided into a single file (for example, x and mx are divided into one x.js file, y and my are divided into one y.js file...)
If you use extends, you will find a problem. Properties cannot be shared, and neither can methods.
Can we combine (x.js / y.js / ...) into A when defining class A?
Mixin
模式简而言之,就是把几个类的
prototype
属性合并,最后那个大的类再继承这个合并了的类就行了。阮一峰的ES6教程有个样例的程序,你可以参考参考:Mixin-模式的实现