Pick a call site
// shared setup
const user = { name: 'Ana', greet() {...} };
const other = { name: 'Bob' };
function User(n) { this.name = n; }
How this resolves
this resolves to
—
One function, six call sites
function whoAmI() {
return this?.name ?? 'undefined';
}
const a = { name: 'A', whoAmI };
const b = { name: 'B' };
Same function, six results. The function never changes — only how it's called. Watch each call site produce a different
this.
Results
Detached Method
Output
// run broken vs fixed to compare…