mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(class-extends): edit class-extends
This commit is contained in:
parent
be281832bf
commit
ce3b9f598b
@ -664,15 +664,29 @@ o.attr === true // false
|
||||
|
||||
## Mixin 模式的实现
|
||||
|
||||
Mixin 模式指的是,将多个类的接口“混入”(mix in)另一个类。它在 ES6 的实现如下。
|
||||
Mixin 指的是多个对象合成一个新的对象,新对象具有各个组成成员的接口。它的最简单实现如下。
|
||||
|
||||
```javascript
|
||||
const a = {
|
||||
a: 'a'
|
||||
};
|
||||
const b = {
|
||||
b: 'b'
|
||||
};
|
||||
const c = {...a, ...b}; // {a: 'a', b: 'b'}
|
||||
```
|
||||
|
||||
上面代码中,`c`对象是`a`对象和`b`对象的合成,具有两者的接口。
|
||||
|
||||
下面是一个更完备的实现,将多个类的接口“混入”(mix in)另一个类。
|
||||
|
||||
```javascript
|
||||
function mix(...mixins) {
|
||||
class Mix {}
|
||||
|
||||
for (let mixin of mixins) {
|
||||
copyProperties(Mix, mixin);
|
||||
copyProperties(Mix.prototype, mixin.prototype);
|
||||
copyProperties(Mix, mixin); // 拷贝实例属性
|
||||
copyProperties(Mix.prototype, mixin.prototype); // 拷贝原型属性
|
||||
}
|
||||
|
||||
return Mix;
|
||||
|
Loading…
x
Reference in New Issue
Block a user