Skip to content

Instantly share code, notes, and snippets.

@xinlc
Created June 15, 2018 06:53
Show Gist options
  • Save xinlc/75b5bd76153e56b705fc93210df1f673 to your computer and use it in GitHub Desktop.
Save xinlc/75b5bd76153e56b705fc93210df1f673 to your computer and use it in GitHub Desktop.
Mixin 指的是多个对象合成一个新的对象,新对象具有各个组成成员的接口.
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