JavaScript: Function.prototype.apply

By Xah Lee. Date: . Last updated: .
f.apply(thisBinding)
evaluate f with this Binding having value of thisBinding.
// example of using Function.prototype.apply
function ff() {
  return this;
}

const jj = {};
console.log(ff.apply(jj) === jj);
f.apply(thisBinding, argList)
elements of the array argList is passed to the function as arguments. argList is a array or Array-Like Object.
// example of using 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);
console.log(jj.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);

JavaScript: Apply Function

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Function

prototype