Redirecting Function Context with call(), apply(), and bind()

A function's this binding is established at invocation time, not during its definition—the calling context determines the value of this. The methods call(), apply(), and bind() provide explicit control over this mechanism.

Using call()

Signature: someFunc.call(thisContext, arg1, arg2, ...)

The function executes immediately, and its internal this references the provided context. Arguments are past individually.

function Person(first, last) {
  this.firstName = first;
  this.lastName = last;
}

const recipient = {};
Person.call(recipient, 'Aria', 'Khan');

// recipient.firstName -> 'Aria'
// recipient.lastName  -> 'Khan'

// When no context is supplied, this reverts to the global object (or undefined in strict mode)
Person.call();
Person.call(null);
Person.call(undefined);

The call() invocation explicitly binds this to recipient, enabling the target object to acquire new properties.

Using apply()

Signature: someFunc.apply(thisContext, [arg1, arg2, ...])

Similar to call(), the funcsion runs immediately with a specified this value, but receives its arguments as an array or array-like object.

function initialize(id, role) {
  this.memberId = id;
  this.accessRole = role;
}

const account = {};
initialize.apply(account, ['AE-2091', 'editor']);

// account.memberId   -> 'AE-2091'
// account.accessRole -> 'editor'

// Without an explicit context, this falls back to the global scope
initialize.apply();
initialize.apply(null);
initialize.apply(undefined);

The apply() approach is especially useful when arguments already exist in array form.

Using bind()

Signature: someFunc.bind(thisContext, arg1, arg2, ...)

Rather than invoking the functon immediately, bind() returns a new function permanently bound to the supplied context. The resulting function can be called later.

function attachMetadata(title, priority) {
  this.label = title;
  this.level = priority;
}

const container = {};
const attachToContainer = attachMetadata.bind(container, 'Urgent', 5);
attachToContainer();

// container.label -> 'Urgent'
// container.level -> 5

// Compact invocation:
// attachMetadata.bind(container, 'Urgent', 5)();

Quick Reference

  • All three methods redirect this to a chosen object.
  • call() and apply() execute the function right away; bind() produces a bound copy for later execution.
  • call() and bind() accept a comma-separated list of arguments, while apply() expects an array.

Tags: javascript this functions context

Posted on Fri, 28 Aug 2026 16:48:05 +0000 by darksniperx