-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathconvert-class.js
39 lines (31 loc) · 1.04 KB
/
convert-class.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
export function fromESClass(ESClass) {
class ExtendedClass extends ESClass {
static extend(proto) {
const subclass = class extends ExtendedClass {
constructor() {
super();
if (proto && proto.init) {
proto.init.apply(this, arguments);
}
}
};
// Copy the prototype so that the constructor is not overwritten
Object.assign(subclass.prototype, proto);
addInstanceGetter(subclass.prototype);
// Apply the prototype to fn to allow for chaining
subclass.fn = subclass.prototype;
return subclass;
}
}
addInstanceGetter(ExtendedClass.prototype);
// Apply the prototype to fn to allow for chaining
ExtendedClass.fn = ExtendedClass.prototype;
return ExtendedClass;
}
function addInstanceGetter(proto) {
Object.defineProperty(proto, '_instance', {
get: function() {
return this;
}
});
}