mercredi 11 octobre 2017

Create class with arguments or call its methods separately

How JS code should be structered when instantiating new classes inside controller class Main.

Solutions:

A: pass arguments while creating new class - new Options(args) - and let Options's constructor call its own methods.

B: create new class and call the classes' methods on the object.

Later I'd use properties from Options in another classes.

// A
class Main {
 constructor(options) {
    this.options = new Options(options);

    { firstProperty, secondProperty } = this.options;
    this.another = new Another(firstProperty, secondProperty);
  }
}

// B
class Main {
 constructor(options) {
    this.options = new Options();
    const firstProperty = this.options.methodA(options);
    const secondProperty = this.options.methodB(options);

    this.another = new Another();
    const anotherPropety = this.other.methodA(firstProperty);
  }
}

Aucun commentaire:

Enregistrer un commentaire