1

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?

3
  • You can extend that class (eg: class AWrapper extends A { ... }) Commented Aug 28, 2019 at 13:30
  • this? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 28, 2019 at 13:30
  • Well I want to only work with the instance a. I.e. assume a is just an instance of A, not A directly. I've edit the example Commented Aug 28, 2019 at 13:33

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

1 Comment

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: AWrappers constructur).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.