Is there a way to easily duplicate all properties and methods of a class instance?
class A {
get prop1() { return 1; }
get prop2() { return 2; }
doStuff() {
return this.prop1 + this.prop2;
}
}
class B extends A {
get prop1() { return 5; }
}
class AWrapper {
constructor(a) {
// [1] Duplicate all methods/properties of a
this.doStuff = () => a.doStuff() + 42;
}
}
const a = new A();
const b = new B();
const wA = new AWrapper(a);
const wB = new AWrapper(b);
console.log(a.prop1(), wA.prop1(), wB.prop1()); // 1, 1, 5
console.log(a.doStuff(), wA.doStuff()); // 3, 45
Instead of manually copying each method/property like in [1]
, is there a simple command to achieve this so that wA
mirrors the structure of a
?