This chapter follows Scope and closures.
In JavaScript, this is a binding that depends on how a function is invoked, not where it is declared. That is unlike lexical variables and unlike Python’s explicit self parameter.
Default binding
A plain function call in non-strict sloppy mode sets this to the global object (window in browsers, globalThis everywhere). In strict mode, a plain call leaves this as undefined.
'use strict';function show() { console.log(this);}show(); // undefinedImplicit binding (method call)
When a function is called as obj.method(), this is obj for the duration of that call—as long as the call stays in method form.
const user = { name: 'Ada', greet() { return this.name; },};user.greet(); // 'Ada'Extracting the method loses the receiver:
const g = user.greet;g(); // not 'Ada' — default bindingExplicit binding
call, apply, and bind set this explicitly. bind returns a new function with a fixed receiver.
function tag() { return this.id;}const ctx = { id: 42 };tag.call(ctx); // 42
const bound = tag.bind(ctx);bound(); // 42Libraries often bind handlers so callbacks keep the right this.
new binding
With new Constructor(), a fresh object is created, linked to the prototype, and this inside the constructor refers to that object until the constructor returns (if it returns an object, that object is used instead).
function Point(x, y) { this.x = x; this.y = y;}const p = new Point(1, 2);Arrow functions and lexical this
Arrow functions do not have their own this. They inherit this from the enclosing lexical scope at creation time—like a closure over the current receiver.
const timer = { seconds: 0, start() { setInterval(() => { this.seconds++; // lexically outer this = timer }, 1000); },};Use arrows for callbacks when you want outer this; use regular functions for methods that must receive dynamic this.
Classes and this
Class methods use the same implicit binding as object methods. In constructors and class fields, this refers to the instance under construction. Unbound methods passed as callbacks still suffer the extraction problem—arrow field initializers or bind in the constructor are common fixes.