Created
June 15, 2018 06:53
-
-
Save xinlc/75b5bd76153e56b705fc93210df1f673 to your computer and use it in GitHub Desktop.
Mixin 指的是多个对象合成一个新的对象,新对象具有各个组成成员的接口.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mix(...mixins) { | |
class Mix {} | |
for (let mixin of mixins) { | |
copyProperties(Mix, mixin); // 拷贝实例属性 | |
copyProperties(Mix.prototype, mixin.prototype); // 拷贝原型属性 | |
} | |
return Mix; | |
} | |
function copyProperties(target, source) { | |
for (let key of Reflect.ownKeys(source)) { | |
if ( key !== "constructor" | |
&& key !== "prototype" | |
&& key !== "name" | |
) { | |
let desc = Object.getOwnPropertyDescriptor(source, key); | |
Object.defineProperty(target, key, desc); | |
} | |
} | |
} | |
// class DistributedEdit extends mix(Loggable, Serializable) { | |
// // ... | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment