简体   繁体   English

对象构建未按预期工作

[英]object building not working as expected

I have 3 different classes A, B, C. 我有3个不同的班级A,B,C。

A is, essentially, the parent of both B and C. When building both B and C object, A has to be included too. A本质上是B和C的父对象。在构建B和C对象时,也必须包含A。

Eventually, the class returns an object, which is currently B (inherits A), or C (inherits A), or just A. 最终,该类返回一个对象,该对象当前是B(继承为A)或C(继承为A)或仅是A。

Initialising of new object: const A = new A("a", "b", "c").json(); 新对象的初始化: const A = new A("a", "b", "c").json(); or const C = new C("a", "b", "c", "cClass1", "cClass2").json(); const C = new C("a", "b", "c", "cClass1", "cClass2").json();

At the moment, I use inheritance to achieve this: 目前,我使用继承来实现这一点:

export class A {
  constructor (option1, option2, option3) {
    this.option1 = option1;
    this.option2 = option2;
    this.option3 = option3;
  }

  json () {
    return {
      key1: this.option1,
      key2: this.option2,
      key3: this.option3,
    };
  }
}

export class B extends A {
  constructor (option1, option2, option3, bClass1, bClass2) {
    super(option1, option2, option3);
    this.bClassKey1 = bClass1;
    this.bClassKey2 = bClass2;
  }

  json () {
    return {
      ...super.json(),
      bClassKey1: this.bClass1,
      bClassKey2: this.bClass2
    };
  }
}

export class C extends A {
  constructor (option1, option2, option3, cClass1, cClass2) {
    super(option1, option2, option3);
    this.cClassKey1 = cClass1;
    this.cClassKey2 = cClass2;
  }

  json () {
    return {
      ...super.json(),
      cClassKey1: this.cClass1,
      cClassKey2: this.cClass2
    };
  }
}

I now need to change how the objects are built, because I need to achieve the following: 现在,我需要更改对象的构建方式,因为我需要实现以下目标:

I need an object that contains all of the classes unique parameters, like so: 我需要一个包含所有类的唯一参数的对象,如下所示:

{
    key1: option1,
    key2: option2,
    key3: option3,
    bClassKey1: bClass1,
    bClassKey2: bClass2,
    cClassKey1: cClass1,
    cClassKey2: cClass2
}

However, I cannot use multiple inheritance in JS (apart from mixin NPM, but I'd rather attempt to achieve it natively). 但是,我不能在JS中使用多重继承(除了mixin NPM之外,我宁愿尝试以本机方式实现它)。

How can I return a object, that's built with A parameters, B parameters (without A) and C parameters (without A). 如何返回由A参数,B参数(无A)和C参数(无A)构建的对象。 However, there's still situations where B and C need to be built, that extends the parent of A. 但是,仍然存在需要构建B和C的情况,从而扩展了A的父代。

It sounds like you want to use aggregation rather than inheritance. 听起来您想使用聚合而不是继承。 Then, each of the classes would have a method that added its information to an object, and you'd use any combination of those methods you wanted. 然后,每个类都将具有一个将其信息添加到对象的方法,并且您可以使用所需的这些方法的任意组合。

 /*export*/ class A { constructor (option1, option2, option3) { this.option1 = option1; this.option2 = option2; this.option3 = option3; } json (target = {}) { // Could also use Object.assign here if **all** enumerable properties are desired target.key1 = this.option1; target.key2 = this.option2; target.key3 = this.option3; return target; } } /*export*/ class B { constructor (bClass1, bClass2) { this.bClassKey1 = bClass1; this.bClassKey2 = bClass2; } json (target = {}) { // Could also use Object.assign here if **all** enumerable properties are desired // (Duplicating the names introduces the opportunity of error, which in fact // there was in the question) target.bClassKey1 = this.bClassKey1; target.bClassKey2 = this.bClassKey2; return target; } } /*export*/ class C { constructor (cClass1, cClass2) { this.cClassKey1 = cClass1; this.cClassKey2 = cClass2; } json (target = {}) { // Could also use Object.assign here if **all** enumerable properties are desired // (Duplicating the names introduces the opportunity of error, which in fact // there was in the question) target.cClassKey1 = this.cClassKey1; target.cClassKey2 = this.cClassKey2; return target; } } const a = new A("option1value", "option2value", "option3value"); const b = new B("bClass1value", "bClass2value"); const c = new C("cClass1value", "cClass2value"); const o = c.json(b.json(a.json())); /* Or, but it requires more temporary objects: const o = {...a.json(), ...b.json(), ...c.json()}; */ console.log(o); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM