From cc95387e4b47a5f51f6e64ebe1c3ab1f22f3640a Mon Sep 17 00:00:00 2001 From: ruanyf Date: Thu, 21 Feb 2019 16:53:41 +0800 Subject: [PATCH] docs(class-extends): fix mixin #820 --- docs/class-extends.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/class-extends.md b/docs/class-extends.md index 6d7c751..5d112c4 100644 --- a/docs/class-extends.md +++ b/docs/class-extends.md @@ -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);