📅 Week 4 — Day 5 JavaScript OOP Basics:
Constructors, new, Prototypes, Inheritance & ES6 Classes
Hey hey, brilliant campers! 👋🌞
Grab your ☕ and settle in—today we level up from “using objects” to
designing them. By the end, you’ll know how to build your own
blueprints for data and behavior, share methods efficiently, and create families of related objects. This is the foundation of building real apps.
1) Why OOP? (Quick intuition)
Think of building many similar things:
➤A
Student with name,grade,age, and methods like introduce() ,takeExam(), playFootball(),or getAverage().
➤A
Product with price, discount logic, etc.
You don’t want to copy-paste the same object structure again and again. You want a
blueprint you can reuse to make many instances. That’s OOP.
Key words you’ll see:
✅
Class / Constructor → the blueprint
✅
Instance → an object created from the blueprint
✅
Method → a function that lives on the object
✅
Prototype → where shared methods live (saves memory!)
✅
Inheritance → one blueprint building on another
2) Constructor Functions (Pre‑class style)
Before ES6 classes, JavaScript used
constructor functions to act like blueprints. Now it's changed. But let's look what it was like since they are very related.
2.1 Defining a constructor
By convention, constructor names are
Capitalized.
function Person(name, age) {
this.name = name; // instance property
this.age = age; // instance property // ❌ Avoid defining methods here (explained below) }
2.2 Creating instances with new
const p1 = new Person("Abebe", 20);
const p2 = new Person("Saba", 22);
console.log(p1.name); // "Abebe"
console.log(p2.age); // 22
3) What does new actually do?
When you write new Person("Abebe", 20) JavaScript does:
➤
Create a new empty object: {}
➤
Link that object to Person.prototype
➤
Bind this inside Person to the new object
➤
Return the object automatically
If you forget new, this won’t point to a new object—bugs ensue.
4) Prototypes: where shared methods live
If you put methods
inside the constructor, each instance gets its own copy (wasteful). Instead, attach methods to the
prototype so all instances share one function.
function Person(name, age) {
this.name = name;
this.age = age; } // Shared by all Person instances
Person.prototype.greet = function () {
console.log(Hi, I'm ${this.name} and I'm ${this.age}.); };
const a = new Person("Almaz", 25);
const b = new Person("Kebede", 30);
a.greet(); // "Hi, I'm Almaz and I'm 25."
b.greet(); // "Hi, I'm Kebede and I'm 30."
console.log(a.greet === b.greet); // true ✅ (one shared function)
4.1 Property lookup (prototype chain)
When you call a.greet():
➥JS looks for greet on a
➥If not found, it looks on Person.prototype
➥If still not found, it looks on Object.prototype
➥…until it either finds it or gives undefined
5) Inheritance with Constructor Functions
Let’s say a Student is a specialized Person.
5.1 Borrow the Person constructor for shared properties
function Student(name, age, major) {
Person.call(this, name, age); // call Person with this instance
this.major = major; }
5.2 Link prototypes so methods are inherited
Student.prototype = Object.create(Person.prototype); // inherit
Student.prototype.constructor = Student; // fix constructor pointer
Student.prototype.study = function () {
console.log(${this.name} is studying ${this.major}.); };
5.3 Use it
const s = new Student("Mahi", 19, "Computer Science");
s.greet(); // from Person.prototype s.study(); // from Student.prototype
That’s classic JavaScript inheritance before classes.