mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2024-04-21 12:32:22 +00:00
docs(class-extends): fix mixin #820
This commit is contained in:
+12
-6
@@ -688,11 +688,17 @@ const c = {...a, ...b}; // {a: 'a', b: 'b'}
|
||||
|
||||
```javascript
|
||||
function mix(...mixins) {
|
||||
class Mix {}
|
||||
class Mix {
|
||||
constructor() {
|
||||
for (let mixin of mixins) {
|
||||
copyProperties(this, new mixin()); // 拷贝实例属性
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let mixin of mixins) {
|
||||
copyProperties(Mix.prototype, mixin); // 拷贝实例属性
|
||||
copyProperties(Mix.prototype, Reflect.getPrototypeOf(mixin)); // 拷贝原型属性
|
||||
copyProperties(Mix, mixin); // 拷贝静态属性
|
||||
copyProperties(Mix.prototype, mixin.prototype); // 拷贝原型属性
|
||||
}
|
||||
|
||||
return Mix;
|
||||
@@ -700,9 +706,9 @@ function mix(...mixins) {
|
||||
|
||||
function copyProperties(target, source) {
|
||||
for (let key of Reflect.ownKeys(source)) {
|
||||
if ( key !== "constructor"
|
||||
&& key !== "prototype"
|
||||
&& key !== "name"
|
||||
if ( key !== 'constructor'
|
||||
&& key !== 'prototype'
|
||||
&& key !== 'name'
|
||||
) {
|
||||
let desc = Object.getOwnPropertyDescriptor(source, key);
|
||||
Object.defineProperty(target, key, desc);
|
||||
|
||||
Reference in New Issue
Block a user