Preparation
Why Classes? The Copy-Paste Problem
const tea1 = { name: "Sencha", pricePerGram: 0.12, origin: "Japan" };
const tea2 = { name: "Earl Grey", pricePerGram: 0.08, origin: "India" };// Calculate price for 100 grams
const price1 = tea1.pricePerGram * 100;
const price2 = tea2.pricePerGram * 100;
// Validate a new tea
function validateTea(tea) {
if (!tea.name) throw new Error("Name required");
if (tea.pricePerGram < 0) throw new Error("Price must be positive");
}The Constructor and this
thisCreating Instances with new
newMethods: Behavior Lives with Data
Static Methods
Inheritance: extends and super()
extends and super()Classes vs Objects
Class
Instance
Summary
Term
Meaning
Pre-Reading (Optional)
Last updated