Notes

Object-Oriented Programming

Encapsulation

Inheritance

class MyClass {}

// is the same as
class MyClass extends Object {}
class Charity {}

class Business {
  toString() {
    return "Give us your money.";
  }
}

class Restaurant extends Business {
  toString() {
    return "Eat at Joe's!";
  }
}

class AutoRepairShop extends Business {}

class Retail extends Business {
  toString() {
    return "Buy some stuff!";
  }
}

class ClothingStore extends Retail {}

class PhoneStore extends Retail {
  toString() {
    return "Upgrade your perfectly good phone, now!";
  }
}

console.log(new PhoneStore().toString()); // 'Upgrade your perfectly good phone, now!'
console.log(new ClothingStore().toString()); // 'Buy some stuff!';
console.log(new Restaurant().toString()); // 'Eat at Joe\'s!'
console.log(new AutoRepairShop().toString()); // 'Give us your money.'
console.log(new Charity().toString()); // [object object]

The nuts and bolts of prototypal inheritance pic

class Parent {
  constructor() {
    this.name = "PARENT";
  }
  toString() {
    return `My name is ${this.name}`;
  }
}

class Child extends Parent {
  constructor() {
    super();
    this.name = "CHILD";
  }
}

const parent = new Parent();
console.log(parent.toString()); // my name is Parent

const child = new Child();
console.log(child.toString()); // my name is Child

Polymorphism


The SOLID Principles Explained

SOLID is an anagram for:

Single-Responsibility Principle

A class should do one thing and do it well

The Liskov Substitution Principle Subtype Requirement: Let ϕ(x) be a property provable about objects x of type T. Then ϕ(y) should be true for objects y of type S where S is a subtype of T.

You can substitute child class objects for parent class objects and not cause errors.

The Other Three


Controlling Coupling with The Law of Demeter

When to ignore the Law of Demeter

document
  .getElementById("that-link")
  .addEventListener("click", (e) => e.preventDefault());