对象构建不按预期工作

时间:2017-11-28 12:11:46

标签: javascript class object inheritance

我有3个不同的班级A,B,C。

A本质上是B和C的父级。当构建B和C对象时,也必须包含A。

最后,该类返回一个对象,该对象当前为B(继承A),或C(继承A),或仅返回A.

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

目前,我使用继承来实现这一目标:

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
    };
  }
}

我现在需要更改对象的构建方式,因为我需要实现以下目标:

我需要一个包含所有类唯一参数的对象,如下所示:

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

但是,我不能在JS中使用多重继承(除了mixin NPM,但我宁愿尝试本地实现它。)

如何返回一个用A参数,B参数(没有A)和C参数(没有A)构建的对象。但是,仍然需要构建B和C,这扩展了A的父级。

1 个答案:

答案 0 :(得分:0)

听起来你想使用聚合而不是继承。然后,每个类都有一个将其信息添加到对象的方法,并且您将使用您想要的那些方法的任意组合。

/*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);