liveBook · Manning

This

As explained in JavaScript in Depth

The JavaScript this value associated with a function invocation, determined by its call-site context.

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();  #B

Run the shared prototype-method example to observe this selecting drawer1 and drawer2 at separate call sites. section 5.4.2

How JavaScript in Depth covers it

How deep it goes
Defines call-site behavior, four regular-function rules, arrow capture, prototype receivers, and common binding failures.
Examples
Uses shared drawer methods, destructured cabinet methods, explicit call/apply, bind, and a browsable prototype listing.
What it assumes
Assumes basic objects and functions while introducing strict mode, prototype receivers, arrow capture, and method extraction.
What you can do after
Diagnose/debug with it by tracing call sites, identifying binding precedence, and restoring receivers with call, apply, or bind.
How it connects
Connects this with regular functions, arrow functions, prototypes, strict mode, destructuring, call, apply, and bind.

Also explained in

Compare how 7 books explain this →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.