Lesson 6 of 9 · 26m
`this`, Property Lookup, and Prototype Chains
The call-site rules for `this`, how prototype lookup differs from class copying, and where method extraction quietly breaks objects.
this is not the object where a function was written. In ordinary functions it is determined by the call site. Prototypes are not class copying either: objects delegate property lookup to another object. Once those two ideas separate, most of the allegedly weird behavior becomes mechanical.
The call site decides
const user = {
name: "Ada",
greet() {
return `hello ${this.name}`;
},
};
user.greet(); // hello Ada — receiver is user
const fn = user.greet;
fn(); // undefined receiver in strict/module codeMethod extraction is the classic failure. Passing user.greet as a callback loses the receiver. Arrow functions are different: they do not bind their own this; they close over the surrounding lexical one.
button.addEventListener("click", user.greet.bind(user));
button.addEventListener("click", () => user.greet());Both fixes are explicit. Choose the arrow when the intent is a closure; choose bind when an API expects a stable function identity you may remove later.
Prototype lookup is delegation
const base = {
describe() { return `id=${this.id}`; },
};
const record = Object.create(base);
record.id = 42;
record.describe(); // id=42When record.describe is evaluated, the engine checks record, then Object.getPrototypeOf(record), then up the chain. When the method executes, this is still record, not base. That combination — lookup up the chain, receiver at the bottom — is the heart of prototype inheritance.
Own properties shadow inherited ones
record.describe = function () {
return "local override";
};This does not mutate base. It creates an own property that stops lookup. The same principle explains why hasOwnProperty exists and why objects used as maps are dangerous when keys such as __proto__ or constructor collide with inherited machinery.
Classes are prototype syntax, not a new object model
class Cache {
constructor(limit) { this.limit = limit; }
full() { return this.size >= this.limit; }
}Methods live on Cache.prototype; fields assigned in the constructor live on each instance. Class syntax adds initialization order details, private fields, and safer construction, but it does not replace prototype delegation with copying.
Field notes
obj.__proto__is legacy accessor syntax; useObject.getPrototypeOfandObject.createin production code.Object.create(null)is a good dictionary precisely because it has no prototype baggage.- Arrow functions as class fields solve callback
thiscapture but create one function per instance. Usually fine in UI objects, measurable in huge object counts. superis not just "parentthis"; method lookup forsuperis tied lexically to the object or class where the method was defined.
Was this lesson helpful?