Echo JS 0.11.0

<~>
tracker1 1697 days ago. link 1 point
This is a pretty good demonstration of the pattern.  I will say that in practice, I would prefer to simply use bare object  syntax over the use of new with constructor functions.  When I do use constructors, I'd now prefer the class syntax.

    class Employee {
      constructor(type, name) {
        this.type = type;
        this.name = name;
      }
    }

    class Developer extends Employee {
      constructor(name) {
        base("Developer", name);
      }
    }

    class Tester extends Employee {
      constructor(name) {
        base("Developer", name);
      }
    }

    class EmployeeFactory {
      static types = [
        Developer,
        Tester,
      }

      create(type, name, ...options) {
        const TypedEmployee = EmployeeFactory.types[type];
        return TypedEmployee && new TypedEmployee(name, ...options);
      }
    }

I did put type before name above, as imho it should be first as this will work better with a spread operator.

Replies