The Apply method is a powerful Function method used by javascript developers. Apply allows you to borrow a function and invoke said function with a given this value.

The Apply method executes a function with an array of parameters or array-like objects. Upon execution each parameter is then passed to the function individually. This makes Apply very useful for variadic functions, which are functions that can take a varying number of arguments.

Let's take a look at a simple example to illustrate how the Apply function works. Similar to javascript's bind() method we can use Apply to explicitly set the value of this when we choose to invoke the function. When a function is invoked using the apply method we set the this value to the object by passing it in as the first parameter:

  
  
function earthling() {  
  var human = {  
     name: "John Doe",
     hello: function() {
       console.log(this.name + " says hello " + arguments[1] + " I'm from " + arguments[0]);
     }
  }
  human.hello.apply(human, arguments);
}

earthling("Earth", "Jahde");  
// "John Doe says hello Jahde I'm from Earth"
  

In the code snippet above human.hello.apply(human, arguments) sets the this value as the human object variable. The arguments in this execution are the parameters passed into the the hello function when it is invoked. When we call earthling("Earth", "Jahde"); the arguments are set as ["Earth", "Jahde"].

One of the most common use cases of the Apply method in our codebase is to borrow functions from the array prototype and use them on array-like objects. Although an Object is not explicitly an Array we can use functions on an object that are common to arrays (which is why we call it an array-like object). As a result we can borrow functions from the Array prototype and use them on array-like objects.

An array-like object must have it's keys defined as non-negative integers. This allows us to invoke prototypical Array functions using the Apply method. The array-like object keys become the index for the "array":

  
  
var arrayLikeObj = {0:"Zidane", 1:"Ronaldo", 2:"Messi", 3:["Henry", "Makelele", "Hazard"] };

var newArray = Array.prototype.slice.apply (arrayLikeObj, arrayLikeObj.keys);  
console.log (newArray);  
// ["Zidane", "Ronaldo", "Messi", ["Henry", "Makelele", "Hazard"]]
  

Using javascript's Apply method, the above example returns a new array based on the initial array-like object of player names. The arraylikeObj keys became the index for the Array prototypical slice method resulting in a new array given the inputs. This technique of using Array prototypical methods is also very useful when we don't have a fixed number of arguments in a function. This concept is known as variable-arity or variadic functions which can take any number of arguments:

Another interesting thing we can do with the Apply method is extend the built-in javascript Math functions for arrays:

  
  
// We can use the Math.max() method​ to find the max number in a set of arguments
console.log (Math.max (7, 29, 33, 52, 40)); // 52

var myNumbers = [7, 29, 33, 52, 40];

// Unfortunately we cannot directly pass an array of #'s to the Math.max method, it will not return the max instead returning NaN
console.log (Math.max (myNumbers)); // NaN

var myNumbers = [7, 29, 33, 52, 40];

// If we use the apply() method, we can explicitly pass an array:​
console.log (Math.max.apply (null, myNumbers)); // 52

>>> which is equivalent to:
Math.max(7, 29, 33, 52, 40)