JSFiddle - React, Tailwind, and code Playground

by 3gwebtrain

JavaScript

class Person {

  constructor(){
    this.companyName = "";
    this.position = "";
    this.income = 0;
  }

  toString(){
    return (`Person works at ${this.companyName} in the position of ${this.position} with the salary ${this.income}`);
  }

}

class PersonBuilder {
    constructor(person = new Person()) {
        this.person = person;
    }

    get lives() {
        return new PersonAddressBuilder(this.person);
    }

    get works() {
        return new PersonJobBuilder(this.person);
    }

    build() {
        return this.person;
    }
}
class PersonJobBuilder extends PersonBuilder {
  constructor(person){
    super(person);
  }

  at(companyName){
    this.companyName = companyName;
    return this;
  }
  asA(position){
    this.position = position;
    return this;
  }
  earning(income){
    this.income = income;
    return this;
  }
}

let pb = new PersonBuilder();
let person = pb
    .works.at("Fabrikam")
    .asA("Engineer")
    .earning(123000)
    .build();
console.log(person.toString());