Bind, Call and Apply
Bind Method
In JavaScript, bind function is used to enhance the implementation of an existing function for that object by creating new function without modifying the original function.
Thebind
method does not immediately invoke the function; instead, it returns a new function with the specifiedthis
value and optionally initial arguments.// Example object const person = { firstName: "John", lastName: "Doe", getFullName: function() { return this.firstName + " " + this.lastName; } }; // Create a new function with a specific 'this' value const getFullNameFunction = person.getFullName.bind(person); // Call the new function console.log(getFullNameFunction()); // Outputs: John Doe const personTwo = { firstName: "Michael", lastName: "Jorden" }; // Create a new function with a specific 'this' value const getFullNameFunctionTwo = person.getFullName.bind(personTwo); // Call the new function console.log(getFullNameFunctionTwo()); // Outputs: Michael Jorden
In this example,
bind
is used to create a new functiongetFullNameFunction
wherethis
is bound to theperson
object. When you later callgetFullNameFunction()
, it uses thethis
context of theperson
object.
You can bind another objectpersonTwo
to get the different result ingetFullNameFunctionTwo()
You can also pass arguments tobind
to pre-set initial values for the parameters,const greet = function(greeting) { return `${greeting}, ${this.firstName} ${this.lastName}!`; }; const greetJohn = greet.bind(person, "Hello"); console.log(greetJohn()); // Outputs: Hello, John Doe!
Here, the
greet
function is bound to theperson
object, and the first argument is set to "Hello." WhengreetJohn()
is called, it uses the bound context and the pre-set argument.Keep in mind that the original function (
getFullName
orgreet
in the examples) is not modified bybind
. Instead,bind
creates a new function with the specified context and, if provided, initial arguments.Call Method
In JavaScript, you can use thecall
method to invoke a function with a specifiedthis
value and arguments provided individually. The basic syntax is as follows,function functionName(arg1, arg2, arg3, ...) { // function implementation } // Using call method functionName.call(thisValue, arg1, arg2, arg3, ...);
Here,
thisValue
is the value that will be used asthis
when the function is executed. The subsequent arguments are the arguments that will be passed to the function.Here's a simple example,
function greet(name) { console.log(`Hello, ${name}! I'm ${this.title}.`); } const person = { title: 'Mr.' }; // Using call method to invoke greet with person as thisValue greet.call(person, 'John');
In this example,
this.title
inside thegreet
function will refer to thetitle
property of theperson
object because we used thecall
method to set thethis
value.Note that the
call
method is just one of several ways to change the context (this
value) of a function in JavaScript. You can also useapply
,bind
, or the arrow function syntax, depending on your requirements and preferences.Apply Method
In JavaScript, theapply
method is similar tocall
but takes an array or array-like object as its second argument, which will be used as the arguments when invoking the function. The basic syntax is as follows,function functionName(arg1, arg2, arg3, ...) { // function implementation } // Using apply method functionName.apply(thisValue, [arg1, arg2, arg3, ...]);
Here's an example,
function greet(name, age) { console.log(`Hello, ${name}! I'm ${this.title}. I'm ${age} years old.`); } const person = { title: 'Mr.' }; // Using apply method to invoke greet with person as thisValue and an array of arguments greet.apply(person, ['John', 30]);
In this example,
this.title
inside thegreet
function will refer to thetitle
property of theperson
object because we used theapply
method to set thethis
value, and the array['John', 30]
is passed as the arguments.Similar to
call
, theapply
method is less commonly used in modern JavaScript, and you might find that using the spread operator (...
) with thecall
method or other alternatives is more common.Difference between Bind, Call and Apply
In JavaScript,bind
,call
, andapply
are methods used to control thethis
context of functions. They all allow you to set the value ofthis
inside a function, but they differ in how they are used and what they return.
i) Call
Thecall
method calls a function with a giventhis
value and arguments provided individually.// Syntax functionName.call(thisArg, arg1, arg2, ...)
// Example function greet(greeting, punctuation) { console.log(greeting + ', ' + this.name + punctuation); } const person = { name: 'Alice' }; greet.call(person, 'Hello', '!'); // Output : Hello, Alice!
ii) Apply
Theapply
method is similar tocall
, but it takes an array (or an array-like object) of arguments instead of listing them one by one.// Syntax functionName.apply(thisArg, [argsArray])
// Example function greet(greeting, punctuation) { console.log(greeting + ', ' + this.name + punctuation); } const person = { name: 'Alice' }; greet.apply(person, ['Hello', '!']); // Output : Hello, Alice!
iii) Bind
Thebind
method creates a new function that, when called, has itsthis
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.// Syntax function.bind(thisArg, arg1, arg2, ...)
// Example function greet(greeting, punctuation) { console.log(greeting + ', ' + this.name + punctuation); } const person = { name: 'Alice' }; const greetPerson = greet.bind(person, 'Hello'); greetPerson('!'); // Output : Hello, Alice!
iv) Key Differences
Invocation -
call
immediately invokes the function with the specifiedthis
value and arguments.apply
immediately invokes the function with the specifiedthis
value and an array of arguments.bind
returns a new function with the specifiedthis
value and initial arguments, but does not immediately invoke the function.Argument Handling -
call
takes arguments as a comma-separated list.apply
takes arguments as an array.bind
takes arguments as a comma-separated list, which can be followed by additional arguments when the new function is called.
When to use what -
Usecall
when you want to invoke the function immediately with a specificthis
value and individual arguments.Use
apply
when you want to invoke the function immediately with a specificthis
value and an array of arguments.Use
bind
when you want to create a new function with a specificthis
value and possibly some initial arguments, to be invoked later.