Prototypes
JavaScript · Reference cheat sheet
Prototypes
JavaScript · Reference cheat sheet
📋 Overview
JavaScript inheritance is prototype-based. Every object has an internal [[Prototype]] link (exposed as __proto__ / Object.getPrototypeOf). Property lookup walks the chain until found or null. Classes are syntax sugar over constructor functions and prototypes.
🔧 Core concepts
- Prototype chain:
obj→Object.getPrototypeOf(obj)→ … →null. - Own vs inherited:
hasOwnProperty/Object.hasOwnvs chain lookup. - Constructor:
Fn.prototypeis assigned as[[Prototype]]ofnew Fn()instances. Object.create(proto): make an object with a chosen prototype.- Class:
class C extends BwiresC.prototype→B.prototype. new.target: detects construct calls.
const proto = { greet() { return "hi"; } };
const obj = Object.create(proto);
obj.greet(); // "hi"💡 Examples
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a noise`;
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.speak = function () {
return `${this.name} barks`;
};
const d = new Dog("Rex");
d.speak(); // "Rex barks"
d instanceof Dog; // true
d instanceof Animal; // true
// Modern equivalent
class Cat extends Animal {
speak() {
return `${this.name} meows`;
}
}
// Inspect
Object.getPrototypeOf(d) === Dog.prototype;
Object.hasOwn(d, "name"); // true
Object.hasOwn(d, "speak"); // false
// Null-prototype map (no inherited keys)
const dict = Object.create(null);
dict.admin = true;
"toString" in dict; // false
// Static vs instance
class Mathy {
static dual(n) {
return n * 2;
}
half() {
return this.value / 2;
}
}// Shadowing
const a = Object.create({ x: 1 });
a.x = 2; // own property shadows prototype
delete a.x; // reveals prototype x again⚠️ Pitfalls
- Mutating built-in prototypes (
Array.prototype) breaks libraries — avoid in app code. for...inenumerates enumerable inherited keys — preferObject.keys/hasOwn.- Setting
__proto__is legacy; useObject.setPrototypeOfsparingly (engine deopts). instanceofcan fail across realms/iframes with different globals.
🔗 Related
- oop.md — class syntax
- objects.md — property model
- new.md — construction
- this_keyword.md — method receivers
- proxy_reflect.md — intercepting lookup