Skip to main content

Command Palette

Search for a command to run...

JavaScript `bind`, `call`, and `apply`: A Detailed Guide

Published
4 min readView as Markdown

In JavaScript, bind, call, and apply are methods available on all functions that allow you to control the context (this) in which a function is executed. These methods are useful in scenarios where the value of this can be ambiguous, such as when passing methods as callbacks or working with event handlers.


1. bind Method

The bind method creates a new function with a specified this value and optional arguments. Unlike call and apply, it does not invoke the function immediately—it returns a new function that can be called later.

Syntax

function.bind(thisArg, ...args)
  • thisArg: The value to bind to this inside the function.
  • ...args: Arguments to partially apply to the function.

Example

const person = {
    name: 'Alice',
    greet: function (greeting) {
        console.log(\`\${greeting}, my name is \${this.name}\`);
    }
};

const greet = person.greet.bind(person);
greet('Hello'); // Output: Hello, my name is Alice

Use Cases

  1. Preserving this Context: Useful when passing methods as callbacks where the this context can be lost:

    const person = {
        name: 'Bob',
        greet: function () {
            console.log(\`Hi, I'm \${this.name}\`);
        }
    };
    
    const greet = person.greet.bind(person);
    setTimeout(greet, 1000); // Output: Hi, I'm Bob
    
  2. Partial Application: Bind can also be used to create partially applied functions:

    function multiply(a, b) {
        return a * b;
    }
    
    const double = multiply.bind(null, 2); // Predefine `a` as 2
    console.log(double(5)); // Output: 10
    

2. call Method

The call method invokes a function immediately with a specified this value and arguments passed individually.

Syntax

function.call(thisArg, arg1, arg2, ...)
  • thisArg: The value to bind to this inside the function.
  • arg1, arg2, ...: Arguments passed to the function.

Example

function introduce(greeting, punctuation) {
    console.log(\`\${greeting}, my name is \${this.name}\${punctuation}\`);
}

const person = { name: 'Alice' };

introduce.call(person, 'Hello', '!'); // Output: Hello, my name is Alice!

Use Cases

  1. Explicit Context Setting: Directly invoke a function with a specific this:

    const person = { name: 'Charlie' };
    
    function sayHi() {
        console.log(\`Hi, \${this.name}\`);
    }
    
    sayHi.call(person); // Output: Hi, Charlie
    
  2. Borrowing Methods: Borrow methods from one object for use on another:

    const person = { name: 'Alice' };
    const anotherPerson = { name: 'Bob' };
    
    function greet() {
        console.log(\`Hello, \${this.name}\`);
    }
    
    greet.call(anotherPerson); // Output: Hello, Bob
    

3. apply Method

The apply method is similar to call, but it takes arguments as an array (or array-like object) instead of individual values.

Syntax

function.apply(thisArg, [argsArray])
  • thisArg: The value to bind to this inside the function.
  • [argsArray]: Arguments passed to the function as an array.

Example

function introduce(greeting, punctuation) {
    console.log(\`\${greeting}, my name is \${this.name}\${punctuation}\`);
}

const person = { name: 'Alice' };

introduce.apply(person, ['Hello', '!']); // Output: Hello, my name is Alice!

Use Cases

  1. Using Arguments from Arrays: When you already have arguments in an array format:

    const numbers = [5, 10, 15, 20];
    console.log(Math.max.apply(null, numbers)); // Output: 20
    
  2. Dynamic Argument Passing: Useful in cases where the number of arguments is variable:

    function sum(...nums) {
        return nums.reduce((a, b) => a + b, 0);
    }
    
    const numbers = [1, 2, 3, 4];
    console.log(sum.apply(null, numbers)); // Output: 10
    

Comparison of bind, call, and apply

Featurebindcallapply
InvocationReturns a new function, not invokedInvokes the function immediatelyInvokes the function immediately
ArgumentsArguments partially applied at bind timePassed individuallyPassed as an array
Return ValueA new functionThe result of the functionThe result of the function

Choosing Between bind, call, and apply

  • Use bind:

    • When you want to create a new function with a specific this value.
    • When you want to partially apply arguments to a function.
  • Use call:

    • When you need to invoke a function immediately with a specific this and arguments passed individually.
  • Use apply:

    • When you need to invoke a function immediately with arguments in an array format.

Practical Example: Combining Them

const person = {
    name: 'Alice',
    introduce: function (greeting) {
        console.log(\`\${greeting}, my name is \${this.name}\`);
    }
};

// Using bind
const boundIntroduce = person.introduce.bind(person);
boundIntroduce('Hello'); // Output: Hello, my name is Alice

// Using call
person.introduce.call({ name: 'Bob' }, 'Hi'); // Output: Hi, my name is Bob

// Using apply
person.introduce.apply({ name: 'Charlie' }, ['Hey']); // Output: Hey, my name is Charlie

These methods provide fine-grained control over function execution and are essential tools for advanced JavaScript development. Let me know if you’d like further clarification or examples! 😊

More from this blog

Khang Nguyen

119 posts