Object Oriented Programming (OOP) allows us to create structured blueprints for our data.
Inheritance
Don't repeat yourself. If you have a Dog and a Cat, they are both Animals. We can use extends to share common logic.
javascript
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + " makes a noise.");
}
}
class Dog extends Animal {
speak() {
// Override the parent method
console.log(this.name + " barks!");
}
}
const d = new Dog("Rex");
d.speak(); // "Rex barks!"Modules (Import/Export)
Before ES6 modules, we had to put everything in one giant file or rely on global variables. Modules act as Firewalls. Variables defined in a module stay in that module unless explicitly exported.