Bad:

    Good:

    1. constructor(age) {
    2. this.age = age;
    3. }
    4. move() { /* ... */ }
    5. }
    6. class Mammal extends Animal {
    7. constructor(age, furColor) {
    8. super(age);
    9. this.furColor = furColor;
    10. }
    11. liveBirth() { /* ... */ }
    12. }
    13. class Human extends Mammal {
    14. constructor(age, furColor, languageSpoken) {
    15. super(age, furColor);
    16. this.languageSpoken = languageSpoken;
    17. }
    18. speak() { /* ... */ }
    19. }

    Bad:

    Good:

    1. this.make = make;
    2. this.model = model;
    3. this.color = color;
    4. }
    5. setMake(make) {
    6. this.make = make;
    7. // NOTE: Returning this for chaining
    8. return this;
    9. }
    10. setModel(model) {
    11. this.model = model;
    12. // NOTE: Returning this for chaining
    13. return this;
    14. }
    15. setColor(color) {
    16. this.color = color;
    17. // NOTE: Returning this for chaining
    18. return this;
    19. }
    20. save() {
    21. console.log(this.make, this.model, this.color);
    22. // NOTE: Returning this for chaining
    23. }
    24. const car = new Car('Ford','F-150','red')
    25. .setColor('pink')
    26. .save();

    You might be wondering then, “when should I use inheritance?” It
    depends on your problem at hand, but this is a decent list of when inheritance
    makes more sense than composition:

    1. Your inheritance represents an “is-a” relationship and not a “has-a”
      relationship (Human->Animal vs. User->UserDetails).
    2. You can reuse code from the base classes (Humans can move like all animals).
    3. You want to make global changes to derived classes by changing a base class.
      (Change the caloric expenditure of all animals when they move).

    Bad:

    1. class EmployeeTaxData {
    2. constructor(ssn, salary) {
    3. this.ssn = ssn;
    4. this.salary = salary;
    5. }
    6. // ...
    7. }
    8. class Employee {
    9. constructor(name, email) {
    10. this.name = name;
    11. this.email = email;
    12. }
    13. setTaxData(ssn, salary) {
    14. this.taxData = new EmployeeTaxData(ssn, salary);
    15. }
    16. // ...
    17. }