Prototype — *Object-Oriented JS*
Prototype — *Object-Oriented JS*
🎯 After reading this lesson
After finishing this lesson, you will be confident in the following three things.
- ▸✅ How the prototype chain + __proto__ works
- ▸✅ Why
classis syntactic sugar over prototypes - ▸✅ Comparing Object.create vs the
newkeyword
Keep the learning goals as a checklist, and close the lesson once you can answer all of them.
What is a prototype?
Key takeaway
JS object-orientation is prototype-based, not class-based. Every object has a parent object (its prototype) and inherits methods from it.
How it differs from other languages
const dog = Object.create(animal); // animal as prototype
dog.speak(); // "Sound" (found on animal)
javascriptconst a = { x: 1 }; // top-level parent — holds x
const b = Object.create(a); // b's parent = a
const c = Object.create(b); // c's parent = b
console.log(c.x); // 1
//
// 🔎 How was c.x found? Walk up the chain:
// ① c itself → no x
// ② c's parent b → no x
// ③ b's parent a → x = 1 found! ✅
//
// 💡 This is the "prototype chain" — how JS looks up properties
javascriptclass Animal {
speak() { console.log("sound"); }
}
class Dog extends Animal { // inherits from Animal
bark() { console.log("Woof woof"); }
}
const d = new Dog();
d.speak(); // "Sound" ← not on Dog → found on Animal
d.bark(); // "Woof woof" ← on Dog → used directly
javascriptDog.prototype.__proto__ === Animal.prototype; // true
d.__proto__ === Dog.prototype; // true
javascriptconst arr = [1, 2, 3];
// 🔎 Who is arr's parent?
console.log(arr.__proto__ === Array.prototype); // true
console.log(arr.map(x => x * 2)); // [2, 4, 6]
// ↑ map is defined on Array.prototype, not on arr itself!
const s = "hello";
console.log(s.__proto__ === String.prototype); // true
console.log(s.toUpperCase()); // "HELLO"
// ↑ toUpperCase is also defined on String.prototype and shared
// 💡 Every array/string "shares" methods → no copy per instance → higher memory efficiency
javascriptfunction Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(${this.name}: sound);
};
const a = new Animal("Puppy");
a.speak(); // "Puppy: sound"
Common pitfalls
1. Arrow functions have no prototype:
2. Directly mutating __proto__ is dangerous:
3. Surprising behavior of for...in:
Summary
- ▸JS = prototype-based object-orientation
- ▸Every object has a parent (prototype)
- ▸
classis syntactic sugar — prototypes underneath - ▸The mechanism behind inheritance and method sharing
⚡ Try it yourself — class inheritance + prototype chain
🤖 Try asking AI this
Knowing the concepts in this lesson lets you give AI specific instructions — not a vague "fix this" but a vocabulary-driven request. That is where token savings begin.
- ▸"Add appropriate TypeScript type annotations to this variable"
- ▸"Unify this mix of === and == to use === consistently and make the intent clear"
- ▸"Add unknown type + a type guard to the JSON.parse result in this code"
Why does this reduce tokens?
Without the concepts, you still have to ask "What does that mean?" after every AI response. Those follow-up questions eat tokens. Learn the concept once and the conversation ends in one turn.