I want a possibility to copy all properties/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] Copy all methods/propertys 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
I could copy each method/property by hand, but is there a simple command for [1]
, such that wA
has the same signature as a
?
Jack Bashford
44.3k11 gold badges56 silver badges83 bronze badges
asked Aug 28, 2019 at 13:29
4 Answers 4
Usually, Proxy
is a tool of choice when working with mixins or decorators:
class A {
get prop1() {
return 1;
}
get prop2() {
return 2;
}
doStuff() {
return this.prop1 + this.prop2;
}
}
class B extends A {
get prop1() {
return 5;
}
}
function decorate(target) {
let mixin = {
doStuff() {
return target.doStuff() + 42;
}
}
return new Proxy(target, {
get(_, prop) {
return (prop in mixin) ? mixin[prop] : target[prop];
}
});
}
const a = new A();
const b = new B();
const wA = decorate(a)
const wB = decorate(b)
console.log(a.prop1, wA.prop1, wB.prop1); // 1, 1, 5
console.log(a.doStuff(), wA.doStuff()); // 3, 45
answered Aug 28, 2019 at 13:45
Sign up to request clarification or add additional context in comments.
Comments
Use the extends keyword and call the parent's (A
) doStuff
with : this.doStuff = () => super.doStuff() + 42;
class A {
get prop1() { return 1; }
get prop2() { return 2; }
doStuff() {
return this.prop1 + this.prop2;
}
}
class AWrapper extends A {
constructor(...args) {
super(...args);
this.doStuff = () => super.doStuff() + 42;
}
}
const a = new A();
const w = new AWrapper(a);
console.log(a.prop1, w.prop1); // 1, 1
console.log(a.doStuff(), w.doStuff()); // 3, 45
answered Aug 28, 2019 at 13:37
Comments
Below should do it.
class A {
get prop1() { return 1; }
get prop2() { return 2; }
doStuff() {
return this.prop1 + this.prop2;
}
}
class AWrapper extends A{
constructor(a) {
super(a);
this.doStuff = () => a.doStuff() + 42;
}
}
const a = new A();
const wA = new AWrapper(a);
const wB = new AWrapper(a);
console.log(a.prop1, wA.prop1, wB.prop1); // 1, 1, 1
console.log(a.doStuff(), wA.doStuff()); // 3, 45
answered Aug 28, 2019 at 13:37
Comments
You need to make a BWrapper
class too - other than that, extends
and super
are all you need:
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 extends A {
constructor(a) {
super();
this.doStuff = () => a.doStuff() + 42;
}
}
class BWrapper extends B {
constructor(b) {
super();
this.doStuff = () => b.doStuff() + 42;
}
}
const a = new A();
const b = new B();
const wA = new AWrapper(a);
const wB = new BWrapper(b);
console.log(a.prop1, wA.prop1, wB.prop1); // 1, 1, 5
console.log(a.doStuff(a), wA.doStuff(wA)); // 3, 4
answered Aug 28, 2019 at 13:39
1 Comment
random314
Hm. In my code I have many instances of
A
. Creating a Wrapper for every one is no option. I just have the concrete instance (a
or b
) and want to feed it into a function (in here: AWrapper
s constructur).Explore related questions
See similar questions with these tags.
lang-js
class AWrapper extends A { ... }
)a
. I.e. assumea
is just an instance of A, not A directly. I've edit the example