-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (58 loc) · 1.87 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Takes in one or more classes and returns a single class with
* all properties of the various parents to be extended.
* @param {...any} classes - All the classes you wish to extend
*/
function multiclass(...classes) {
/**
* Class frame to hold all parent properties.
*/
class Class {
constructor(...options) {
// Loop over all incoming classes
for (let baseClass of classes) {
// check if this class already extends another class
let parent = Reflect.getPrototypeOf(baseClass);
// Push the parent onto the classes array if it's a class and if it isn't yet there
if (parent.prototype && !classes.includes(parent)) {
classes.push(parent);
}
// Grab a list of the properties attached to the class; methods, variables, &c.
const properties = Reflect.ownKeys(baseClass.prototype);
// loop through our properties
for (let property of properties) {
// If it's a constructor, we'll pass it our incoming arguments
if (property === "constructor") {
Object.assign(
Class.prototype,
new baseClass.prototype.constructor(...options),
);
// look for static methods or variables on the class
let staticProperties = Reflect.ownKeys(
baseClass.prototype.constructor,
).filter((property) =>
!["length", "prototype", "name"].includes(property)
);
// attach these static properties to the class frame
for (let staticProperty of staticProperties) {
Class[staticProperty] = baseClass[staticProperty];
}
} else {
// otherwise, we'll just attach it to our Class frame
let propertyDescriptor = Reflect.getOwnPropertyDescriptor(
baseClass.prototype,
property,
);
Reflect.defineProperty(
Class.prototype,
property,
propertyDescriptor,
);
}
}
}
}
}
return Class;
}
module.exports = multiclass;