Course 01 · Lesson 01/Objects & classes

Course 01 · Lesson 01 · APIE · OOP principles

Objects & classes

What it means

Object-oriented programming organizes software around objects: focused units that hold data and provide the behavior that works with it. Classes give us a reusable blueprint for creating those objects.

APIE Abstraction · Polymorphism · Inheritance · Encapsulation

02 — Applied

Put behavior beside the state it governs

Example context

A Product class defines a name, price, and discount behavior. Each product in a catalog is a separate object with its own values.

×Data with logic elsewhere
const product = { price: 100 }

function discountedPrice(p, rate) {
  return p.price * (1 - rate)
}
A cohesive object
class Product {
  constructor(private price: number) {}

  discountedBy(rate: number) {
    return this.price * (1 - rate)
  }
}