docs(class-extends): fix mixin #820

This commit is contained in:
ruanyf
2019-02-21 16:53:41 +08:00
parent 679bb51386
commit cc95387e4b
+12 -6
View File
@@ -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);