JS: Function.prototype.apply ❌

By Xah Lee. Date: . Last updated: .

🟢 TIP: better is Reflect.apply.

f.apply(thisBinding)

evaluate f with this (binding) having value of thisBinding.

/*
demo of what Function.prototype.apply does.
*/

// define a function that returns the implicit argument this-binding.
function ff() { return this; }

// create a object
const jj = {};

// use ff.apply(jj)
console.log(ff.apply(jj) === jj);
// true
f.apply(thisBinding, argList)
  • Elements of the array argList is passed to the function as arguments.
  • argList is a array or Array-Like Object.
// demo of Function.prototype.apply

function ff(a, b) {
 this.x = a;
 this.y = b;
}

const jj = {};

ff.apply(jj, [7, 8]);

console.log(jj);
// { x: 7, y: 8 }
// Second arg of Function.prototype.apply can be Array-Like Object

function ff(a, b) {
  this.x = a;
  this.y = b;
}

const jj = {};

ff.apply(jj, { 0: 7, 1: 8, length: 2 });

console.log(jj.x === 7);
console.log(jj.y === 8);

Purpose of Function.prototype.apply

Function.prototype.apply does two things.

One, it lets you pass an explicit value of this-binding to a function. This is similar to Function.prototype.call.

Two, it lets you call a function by passing array as arguments. For example, you have a array xarr = [1,2,3], you want to call plus(1,2,3). Typically, this is done in functional programing languages by a apply function apply(plus, xarr).

Function.prototype.apply was most used to find max item in a array, like this, before 2015.

const xarray = [24, 185, 53];
console.log(Math.max.apply(null, xarray));
// 185

// today, it's easier using spread operator
console.log(Math.max(...[24, 185, 53]));
// 185

Today (2026), there is not much use for Function.prototype.apply.

JavaScript. Apply Function