The this Keyword (call, apply & bind)

Published by Brayan K

this is the single most misunderstood word in JavaScript — and once it clicks, half of the "weird" bugs you've hit suddenly make sense. The trick: this is not decided by where a function is written, but by how it is called.

Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

An ordinary method call supplies its receiver

The call-site rule in the introduction describes ordinary, unbound functions. Arrow functions inherit an enclosing this, and bound functions retain a chosen receiver for ordinary calls. Begin by tracing a normal method call: the receiver is the object used to access the function at that call.

"use strict";
function readLabel() {
  // The caller supplies this; the function reads its label.
  return this.label;
}
const first = { label: "warehouse", read: readLabel };
const second = { label: "shop", read: readLabel };
console.log(first.read());
console.log(second.read());
console.log(first.read === second.read);

// ✅ Expected output:
// warehouse
// shop
// true

Both properties hold the same function, but each call supplies a different receiver. A function does not acquire a permanent owner merely by being stored on an object. Arrays and Objects owns object properties; Functions owns ordinary parameters and return values.

Extracting a method removes the property call

Reading an object property and calling through that property are different operations. After assigning a method to a standalone variable, calling that variable does not retain the original object as a receiver. These examples use strict mode so a plain function call has undefined as this instead of a host-dependent global object.

"use strict";
function hasNoReceiver() {
  return this === undefined;
}
const record = { inspect: hasNoReceiver };
const inspect = record.inspect;
console.log(record.inspect());
console.log(inspect());

// ✅ Expected output:
// false
// true
"use strict";
const record = {
  label: "warehouse",
  read() {
    return this.label;
  }
};
const read = record.read;
console.log(read());

// ✅ Expected error:
// TypeError: Cannot read properties of undefined (reading 'label')

The failure is at the property read inside the function, not at the assignment that extracted it. If the function did not use this, the same plain call could be perfectly valid. Inspect the call and the body together.

A callback receives the calling convention of its API

Passing a method as a value does not carry the property-call syntax with it. This helper deliberately invokes its callback as a plain function, making the lost receiver reproducible without timers or browser-specific behaviour.

"use strict";
function invoke(callback) {
  return callback();
}
const record = {
  label: "warehouse",
  read() {
    return this.label;
  }
};
console.log(invoke(record.read));

// ✅ Expected error:
// TypeError: Cannot read properties of undefined (reading 'label')
"use strict";
function invoke(callback) {
  return callback();
}
const record = {
  label: "warehouse",
  read() {
    return this.label;
  }
};
console.log(invoke(() => record.read()));

// ✅ Expected output:
// warehouse

Different APIs can supply different receivers or accept an explicit thisArg. Do not assume every callback is called like invoke above; Events owns the receiver and event details for DOM listeners.

Call and apply invoke immediately with a chosen receiver

Call runs the function now, using its first argument as the receiver and the remaining arguments as ordinary parameters. Apply also runs now, but takes the parameter values from an array-like argument list. Neither method rewrites the function into a permanently bound one.

"use strict";
function labelFor(prefix, suffix) {
  return prefix + this.label + suffix;
}
const record = { label: "warehouse" };
console.log(labelFor.call(record, "From ", "."));
console.log(labelFor.apply(record, ["From ", "."]));

// ✅ Expected output:
// From warehouse.
// From warehouse.
"use strict";
const first = {
  count: 3,
  add(step) {
    this.count += step;
    return this.count;
  }
};
const second = { count: 10 };
console.log(first.add.call(second, 2));
console.log(first.count);
console.log(second.count);

// ✅ Expected output:
// 12
// 3
// 12

The borrowed call changes second because second is the supplied receiver. The original object keeps its own count. Call and apply do not validate that the receiver contains sensible data; the function still has an input contract.

Bind creates a reusable function without running the body

Bind returns a new function. When that function is called ordinarily, it invokes the original with the stored receiver and any leading arguments you supplied. Creating it does not call the body, and it does not change the original function.

"use strict";
let calls = 0;
function labelFor(prefix, suffix) {
  calls += 1;
  return prefix + this.label + suffix;
}
const record = { label: "warehouse" };
const later = labelFor.bind(record, "From ");
console.log(calls);
console.log(later("."));
console.log(calls);
console.log(labelFor.call({ label: "shop" }, "To ", "!"));

// ✅ Expected output:
// 0
// From warehouse.
// 1
// To shop!
"use strict";
function readLabel() {
  return this.label;
}
const first = { label: "warehouse" };
const second = { label: "shop" };
const readFirst = readLabel.bind(first);
console.log(readFirst());
console.log(readFirst.call(second));
console.log(readFirst.bind(second)());

// ✅ Expected output:
// warehouse
// warehouse
// warehouse

This rule is about ordinary calls. Constructing a bound function with new has a different receiver rule, covered below. Also keep a bound function when an API later needs the same function identity: calling bind again creates another function.

"use strict";
function readLabel() {
  return this.label;
}
const record = { label: "warehouse" };
const saved = readLabel.bind(record);
console.log(saved === saved);
console.log(readLabel.bind(record) === readLabel.bind(record));

// ✅ Expected output:
// true
// false

An arrow uses the surrounding this rather than a new receiver

An object literal does not create a this binding for arrows written inside it. In this factory, a plain strict-mode call gives the factory an undefined receiver, and the returned arrow retains that surrounding value. Calling it as a property cannot turn it into a normal method.

"use strict";
function makeRecord() {
  return {
    label: "warehouse",
    read: () => this.label
  };
}
const record = makeRecord();
console.log(record.read());

// ✅ Expected error:
// TypeError: Cannot read properties of undefined (reading 'label')
"use strict";
const record = {
  count: 3,
  makeReader() {
    return () => this.count;
  }
};
const read = record.makeReader();
console.log(read());
record.count = 8;
console.log(read.call({ count: 100 }));

// ✅ Expected output:
// 3
// 8

The enclosing method was called on record, so the returned arrow reads that record even when called later with call. It reads the current count, not a frozen copy of the number. Closures and Scope owns retained bindings and lifetime beyond this receiver example.

Construction supplies a new object

A constructable function called with new receives a new object as this. A bound receiver is ignored for that construction, although bound leading arguments still apply. Prototypes and Classes owns constructors, prototype links and class inheritance.

"use strict";
function Box(label) {
  this.label = label;
}
const existing = { label: "existing" };
const MakePackedBox = Box.bind(existing, "packed");
const created = new MakePackedBox();
console.log(created.label);
console.log(existing.label);
console.log(created === existing);

// ✅ Expected output:
// packed
// existing
// false

An arrow is not made constructable by storing it on an object or binding it. The distinction here is the invocation form and function kind; choosing new is not a general repair for a missing method receiver.

Complete a later call and an immediate call

The add function reads a count from its receiver. Fill one blank to make a reusable function with a leading step of two, and the other to invoke add immediately with a step of four. The two operations should remain visibly separate.

"use strict";
function add(step) {
  return this.count + step;
}
const record = { count: 7 };
const later = add.___(record, 2); // 👉 replace ___ with bind
console.log(later());
console.log(add.___(record, 4)); // 👉 replace ___ with call

// ✅ Expected output:
// 9
// 11

Pass a counter operation through a plain callback

Create a counter with value three and an increase(step) method that updates and returns its value. Make a bound increase function, then pass it to a helper that invokes its callback plainly. Call through the helper with two and then four, and finally print the counter value. The helper must not know which object the operation belongs to.

"use strict";
function runOperation(operation, amount) {
  return operation(amount);
}
const counter = {
  value: 3,
  increase(step) {
    this.value += step;
    return this.value;
  }
};
const increase = counter.increase.bind(counter);
console.log(runOperation(increase, 2));
console.log(runOperation(increase, 4));
console.log(counter.value);

// ✅ Expected output:
// 5
// 9
// 9

The helper supplies only an ordinary argument. The bound function supplies the receiver when it calls the original method. That separation is what lets the operation survive extraction without making the helper depend on the counter object.

References: MDN documents this and callback conventions, bind, and arrow functions.

Practice quiz

What does this complete program print? "use strict"; function read() { return this.label; } const a = { label: "A", read }; const b = { label: "B", read: a.read }; console.log(a.read(), b.read());

  • A B
  • A A
  • B B
  • undefined undefined

Answer: A B. The two property calls supply different receivers even though both properties hold the same function.

What does this complete program print? "use strict"; function check() { return this === undefined; } const obj = { check }; const plain = obj.check; console.log(plain());

  • false
  • true
  • undefined
  • TypeError: plain is not a function

Answer: true. A plain strict-mode call supplies undefined as this; extracting the function does not retain obj.

What does this complete program print? "use strict"; let calls = 0; function run() { calls++; } const later = run.bind({}); console.log(calls);

  • 1
  • undefined
  • 0
  • TypeError: run is not a function

Answer: 0. Bind creates a function without invoking the original body.

What does this complete program print? "use strict"; function add(value) { return this.base + value; } console.log(add.call({ base: 4 }, 3));

  • 43
  • 3
  • undefined
  • 7

Answer: 7. Call supplies the receiver first and then the ordinary argument.

What does this complete program print? "use strict"; function join(left, right) { return left + this.name + right; } console.log(join.apply({ name: "Mia" }, ["Hi ", "!"]));

  • Hi Mia!
  • Hi undefined!
  • MiaHi !
  • TypeError: CreateListFromArrayLike called on non-object

Answer: Hi Mia!. Apply takes the function arguments from the supplied array.

What does this complete program print? "use strict"; function read() { return this.name; } const fixed = read.bind({ name: "Mia" }); console.log(fixed.call({ name: "Bo" }));

  • Bo
  • Mia
  • undefined
  • TypeError: Cannot read properties of undefined (reading 'name')

Answer: Mia. An ordinary call to a bound function retains its stored receiver.

What does this complete program print? "use strict"; const obj = { value: 2, make() { return () => this.value; } }; const read = obj.make(); console.log(read.call({ value: 9 }));

  • 9
  • undefined
  • 2
  • TypeError: read is not a function

Answer: 2. The arrow retains the this of make; call cannot replace that lexical receiver.

What does this complete program print? "use strict"; function Box(label) { this.label = label; } const old = { label: "old" }; const Bound = Box.bind(old, "new"); const created = new Bound(); console.log(created.label, old.label);

  • new new
  • old old
  • old new
  • new old

Answer: new old. Construction creates a new receiver and ignores the bound receiver, while keeping the bound argument.

Continue this course

Related lessons