The call(), apply(), and bind() methods in JavaScript are used to change the value of the this keyword within a function. The this keyword refers to the object that the function is called on. By changing the value of this, you can make the function behave as if it were called on a different object.
The call() method takes two arguments: the first argument is the value of this, and the second argument is an array of arguments to be passed to the function. For example, the following code calls the myFunction() function with the window object as this and the value 1 as the only argument:
Code snippet
window.myFunction.call(window, 1);
The apply() method is similar to the call() method, but it takes an array of arguments instead of a single argument. For example, the following code calls the myFunction() function with the window object as this and the values 1 and 2 as the two arguments:
Code snippet
window.myFunction.apply(window, [1, 2]);
The bind() method creates a new function that has the specified value of this. The new function can then be called like any other function. For example, the following code creates a new function that has the window object as this and then calls the new function:
The main difference between the call(), apply(), and bind() methods is how they take arguments. The call() method takes the value of this and an array of arguments, the apply() method takes an array of arguments and the value of this, and the bind() method takes the value of this and returns a new function.
Here is a table summarizing the differences between the call(), apply(), and bind() methods:
Method
Arguments
Description
call()
this value, array of arguments
Calls the function with the specified value of this and the specified arguments.
apply()
Array of arguments, this value
Calls the function with the specified value of this and the specified arguments.
bind()
this value
Returns a new function with the specified value of this.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The call(), apply(), and bind() methods in JavaScript are used to change the value of the this keyword within a function. The this keyword refers to the object that the function is called on. By changing the value of this, you can make the function behave as if it were called on a different object.
The call() method takes two arguments: the first argument is the value of this, and the second argument is an array of arguments to be passed to the function. For example, the following code calls the myFunction() function with the window object as this and the value 1 as the only argument:
Code snippet
The apply() method is similar to the call() method, but it takes an array of arguments instead of a single argument. For example, the following code calls the myFunction() function with the window object as this and the values 1 and 2 as the two arguments:
Code snippet
The bind() method creates a new function that has the specified value of this. The new function can then be called like any other function. For example, the following code creates a new function that has the window object as this and then calls the new function:
Code snippet
The main difference between the call(), apply(), and bind() methods is how they take arguments. The call() method takes the value of this and an array of arguments, the apply() method takes an array of arguments and the value of this, and the bind() method takes the value of this and returns a new function.
Here is a table summarizing the differences between the call(), apply(), and bind() methods: