-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.js
More file actions
38 lines (29 loc) · 785 Bytes
/
reflection.js
File metadata and controls
38 lines (29 loc) · 785 Bytes
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
function bindThis(clazz, self) {
const methodKeys = Reflect.ownKeys(clazz.prototype).filter(k => k !== "constructor");
for (const k of methodKeys) self[k] = clazz.prototype[k].bind(self);
}
class Parent {
constructor() {
this.parentVal = "PARENT";
console.log(this);
bindThis(Parent, this);
}
parentMethod() {
console.log(this.parentVal);
}
}
class Child extends Parent {
constructor() {
super();
this.childVal = "CHILD";
bindThis(Child, this);
}
childMethod() {
console.log(this.childVal);
}
}
const c = new Child();
//console.log(Reflect.ownKeys(Reflect.getPrototypeOf(c)), Reflect.getPrototypeOf(c))
c.parentMethod();
c.parentMethod.apply({});
c.childMethod.apply({});