Chapter XXI · The `this` Keyword

apply

The `apply` method invokes a function with a specified `this` value and arguments passed as an array collection.

01 How It Works

Think of packing loose items into a box cargo container before handing it over to a station.

Identical to `call`, except arguments are bundled into an array rather than listed individually.

example.js
function sum(a, b) {
  return a + b + this.extra;
}
sum.apply({ extra: 5 }, [10, 20]); // 35
  1. 1. Target a function reference.

  2. 2. Call `.apply(contextObj, [arrayArgs])`.

  3. 3. Function executes instantly with explicit bindings and array arguments.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
const numbers = [5, 10, 15];
const max = Math.max.apply(null, numbers); // 15

Key Takeaway

Use `apply` when arguments are already structured or collected inside an array.

Related Concepts