1
votes

JavaScript - Méthode de copie de l'instance

Je veux une possibilité de copier toutes les propriétés / méthodes d'une classe instance:

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
class A {
    get prop1() { return 1; }
    get prop2() { return 2; }

    doStuff() {
        return this.prop1 + this.prop2;
    }
}

class B extends A {
   get prop1() { return 5; }
}

Je pourrais copier chaque méthode / propriété à la main, mais existe-t-il une commande simple pour [1] , telle que wA a la même signature que a ?


3 commentaires

Vous pouvez étendre cette classe (par exemple: class AWrapper extend A {...} )


ce? developer.mozilla.org/en-US/docs/ Web / JavaScript / Référence /…


Eh bien, je veux travailler uniquement avec l'instance a . C'est à dire. Supposons que a est juste une instance de A, pas A directement. J'ai édité l'exemple


4 Réponses :


0
votes

Utilisez le mot clé extends et appeler le parent ( A ) doStuff avec: 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


0 commentaires

0
votes

Ci-dessous devrait le faire.

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


0 commentaires

0
votes

Vous devez également créer une classe BWrapper - à part cela, extend et super sont tout ce dont vous avez besoin:

p>

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 commentaires

Hm. Dans mon code, j'ai plusieurs instances de A . Créer un wrapper pour chacun n'est pas une option. J'ai juste l'instance concrète ( a ou b ) et je veux la nourrir dans une fonction (ici: la construction de AWrapper ).



2
votes

Habituellement, Proxy code > est un outil de choix lorsque vous travaillez avec des mixins ou des décorateurs:

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


0 commentaires