A method can work correctly for one object and fail after being extracted into a variable. The reason is that regular functions do not remember where they were written: their this value is selected at each call site. Methods and objects remain loosely coupled until invocation. section 5.4 section 6.3.2
For regular functions, JavaScript resolves this using four precedence rules: new binding, explicit binding through call or apply, implicit method binding, and default binding. Default binding yields globalThis in sloppy mode and undefined in strict mode; each call is evaluated afresh. section 5.4.1 section 6.3.1
Use these distinctions when tracing a surprising this value:
- Calling object.method() gives the object before the dot as this. section 6.3.1
- Calling method.call(object) or method.apply(object, args) supplies this explicitly; bind creates a function that preserves the selected object. section 6.3.1 section 6.3.2
- Calling a method after destructuring removes its object relationship, so default binding applies. section 6.3.2
- Arrow functions capture this from their surrounding scope and do not acquire a new value from their call site. section 6.3
- Prototype lookup does not change the receiver: a shared method found on a prototype receives the original object as this. section 5.4.2
A shared method and a lost receiver
A prototype can define open() once while drawer1.open() and drawer2.open() use each drawer's own label through this. Extracting identify from cabinet and calling identify() loses that receiver; identify.call(cabinet) or identify.bind(cabinet) restores it. section 5.4.2 section 6.3.2
const drawerPrototype = {
open() {
console.log(`Opening the ${this.label} drawer`);
this.isOpen = true;
}
};
const drawer1 = Object.create(drawerPrototype);
drawer1.label = 'tax documents';
const drawer2 = Object.create(drawerPrototype);
drawer2.label = 'receipts';
drawer1.open(); #A
drawer2.open(); #BRun the shared prototype-method example to observe this selecting drawer1 and drawer2 at separate call sites. section 5.4.2






