Apply Method in JavaScript
The apply method in JavaScript is a versatile function method that allows you to invoke a function with a specified this context and an array of arguments. It is particularly useful for dynamic function invocation where the context and arguments can be specified at runtime. Additionally, in functional programming, apply can enhance function composition by addressing argument order issues and utilizing currying.
Function Invocation with apply
The apply method is defined as a function method that enables you to call a function with a given this value and arguments provided as an array. This is particularly useful when you need to control the this context dynamically, such as when invoking callback functions.
Usage Example
skulk.apply(ninja);
In this example, ninja is the this value, and the arguments are passed as an array to the skulk function.
Detailed Example with apply and call
To better understand the apply method, consider the following example that also includes the call method. Both methods are used to supply the function context, but they differ in how they accept arguments.
function juggle() {
var result = 0;
for (var n = 0; n < arguments.length; n++) {
result += arguments[n];
}
this.result = result;
}
var ninja1 = {};
var ninja2 = {};
juggle.apply(ninja1, [1, 2, 3, 4]);
juggle.call(ninja2, 5, 6, 7, 8);
assert(ninja1.result === 10, "juggled via apply");
assert(ninja2.result === 26, "juggled via call");
- Using
apply: We passninja1as the function context and an array of arguments[1, 2, 3, 4]. - Using
call: We passninja2as the function context and a list of arguments5, 6, 7, 8.
The difference between apply and call lies in how the arguments are supplied. apply takes an array of arguments, while call takes a list of arguments.
Figures


These figures illustrate how the apply and call methods work in terms of setting the function context and passing arguments.
Functional Programming with apply
In the context of functional programming, the apply function can be used to enhance function composition by addressing the argument order issue and utilizing currying. This is particularly useful when working with functors, allowing you to apply a function to a functor seamlessly.
Definition and Usage
The apply function can be defined using currying, which is a technique that transforms a function with multiple arguments into a sequence of functions, each with a single argument.
const apply = curry((fn, F) => map.call(F, fn));
This definition uses currying to improve the composition of functions, making it easier to work with functors. The apply function takes a function fn and a functor F, and applies fn to the value inside F.
Example
Consider the following example where apply is used to double a number inside a Success functor:
apply(x => x * 2)(Success.of(2)); // Success.of(4)
In this example, the function x => x * 2 is applied to the value 2 inside the Success functor, resulting in Success.of(4).
Composing Functors
One of the significant advantages of using apply is that it allows for the composition of functors without the need for additional composition functions like composeM.
compose(
apply(x => x ** 2),
apply(x => x * 2)
)(Success.of(2)); // Success(16)
In this composition, the value 2 inside the Success functor is first doubled to 4, and then squared to 16, resulting in Success(16).
By using apply, you can create more readable and maintainable code, especially as the complexity of your logic increases. This approach aligns closely with what you might achieve using external libraries, providing a powerful tool for functional programming in JavaScript.
For more detailed information, refer to Secrets of the JavaScript Ninja, Second Edition and The Joy of JavaScript.
| Book Title | Usage of apply | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|----------------|-----------------|-------------------------------|---------------|-----------------------|
| [Secrets of the JavaScript Ninja, Second Edition](https://www.manning.com/books/secrets-of-the-javascript-ninja-second-edition) | Discusses `apply` as a method to invoke functions with a specified `this` context and array of arguments. | Provides a detailed explanation of `apply` and `call`, including their differences in argument handling. | Connects `apply` to function context manipulation and compares it with `call`. | Includes examples with `juggle` function and figures illustrating `apply` and `call`. | Highlights the use of `apply` for dynamic function invocation and context control. [more](https://www.manning.com/books/secrets-of-the-javascript-ninja-second-edition) |
| [The Joy of JavaScript](https://www.manning.com/books/the-joy-of-javascript) | Describes `apply` as a higher-order function for function composition, addressing argument order and currying. | Explains `apply` in the context of currying and functors, enhancing function composition. | Links `apply` to functor composition and functional programming concepts. | Provides examples using `apply` with functors like `Success` to demonstrate function application. | Emphasizes `apply` for creating readable and maintainable functional code. [more](https://livebook.manning.com/book/the-joy-of-javascript/chapter-5/313) |
FAQ (Frequently asked questions)
What is the purpose of the apply function in JavaScript?
How does the apply function work with functors?