-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmixin.js
36 lines (27 loc) · 829 Bytes
/
mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default function mix(...mixins) {
class Mix {
constructor(...args) {
this.initialize && this.initialize(...args)
for (let i in mixins) {
const newMixin = new mixins[i](...args)
copyProperties(this, newMixin)
copyProperties(this.prototype, newMixin.prototype)
}
}
}
for (let i in mixins) {
const mixin = mixins[i]
copyProperties(Mix, mixin)
copyProperties(Mix.prototype, mixin.prototype)
}
return Mix
}
function copyProperties(target = {}, source = {}) {
const ownPropertyNames = Object.getOwnPropertyNames(source)
ownPropertyNames
.filter(key => !/^(prototype|name|constructor)$/.test(key))
.forEach(key => {
const desc = Object.getOwnPropertyDescriptor(source, key)
Object.defineProperty(target, key, desc)
})
}